[Assembly] 我写的一个汇编冒泡排序

昨天看到版上有个人问冒泡排序的问题,心血来潮也想写一个。好久没有写汇编了,最近想好好学一下。也做了很多准备,下载了一些编译器。

 

可是当我写出来以后不知道怎么编译了,因为我原来所学的都是dos汇编,和现在主流的32位不一样。后来虽然用masm32(带有link16)编译和链接成功,并且能够运行在dosbox中,但是没找到合适的调试工具,找到的16位调试器也都说格式不匹配。后来终于下载了一套(masm,link,debug)16位工具。

 

或许这么古老的东西大家都不怎么用了,不过我觉得既然想好好学,就从头开始吧,中间边查指令手册边写,发现还是有很多限制,例如loop的距离不能太长啊,乘除都要用ax啊等等,现在的限制应该没有那么多了。

 

算作我重新开始汇编走的第一步吧。

 

_DATA segment NumArray DW 100 dup(?) NumStr DB 5 dup(?),' ','$' Msg1 DB 'Please input array of numbers you want to sort( less than 100): ',0dH,0aH,'$' Msg2 DB 'Sorted Array: ',0dH,0aH,'$' Ten DW 10 Minus1 DW -1 _DATA ends _CODE segment assume cs:_CODE, ds:_DATA main proc ;=================initialization=============================== push ds push bp mov ax, _DATA mov ds, ax ;put _DATA into ds lea dx, Msg1 mov ah, 9 ;display Msg1 int 21H xor cx, cx ; store number xor bx, bx ; store index xor dx, dx ; use for multiply xor bp, bp ; store flag ;===============scanf characters from console======================= input_num: mov ah, 1 ; read a char from console ==>al int 21H cmp al, 0dH ; al == return je last_input cmp al, ' ' ; al == space je store_num cmp al, '-' ; al == minus je negative cmp al, '+' ; al == positive sign je input_num or bp, 01H ; al belongs [0-9], set bit 0 of bp sub al, '0' ; change char to value xor ah, ah ; ah = 0 xchg ax, cx ; cx = cx * 10 + ax imul Ten add cx, ax jmp input_num store_num: test bp, 01H ; bp and 1 == 1 means there is a num before space jz fake_input ; if there's no number before space test bp, 02H ; bp and 2 == 2 means the number is negative jz positive ; if not negative not cx ; else multiply -1 add cx, 1 positive: mov NumArray[bx], cx ; store cx to memory add bx, 2 ; each element take 2 bytes xor cx, cx fake_input: test bp, 04H ; bp and 4 == 4 means there's a return, so end input jnz bubble_sort xor bp, bp ; clear bp for next input jmp input_num negative: or bp, 02H ; set bit 1 of bp jmp input_num last_input: or bp, 04H ; set bit 2 of bp jmp store_num ;================bubble sort=================================== bubble_sort: mov cx, bx ; store end position of array xor bx, bx ; clear bx, bx is outer iterator xor bp, bp ; clear bp, bp is inner iterator Loop1: cmp bx, cx ; outer loop compare jnl OutLoop1 mov dx, NumArray[bx] ; read to dx mov bp, bx mov ax, bx Loop2: add bp, 2 cmp bp, cx ; inner loop compare jnl OutLoop2 cmp dx, NumArray[bp] ; find a minimum value after NumArray[bx] jng Loop2 ; record its position to ax mov dx, NumArray[bp] mov ax, bp jmp Loop2 OutLoop2: cmp ax, bx je NoChange xchg dx, NumArray[bx] ; exchange the values xchg ax, bx xchg dx, NumArray[bx] xchg ax, bx NoChange: add bx, 2 jmp Loop1 OutLoop1: ;===================print the result========================== lea dx, Msg2 ; show Msg2 mov ah, 09H int 21H shr cx, 1 ; because the array element take 2 bytes, so cx/2 is the real count of array xor bx, bx ; clear bx print_num: mov si, 4 ; there's 5 byte to store num, assume the number not exceed 32767 mov di, NumArray[bx] ; read a element to di cmp di, 0 ; if di < 0 then print a '-', and di = -di jge positive1 mov dl, '-' mov ah, 02H int 21H not di add di, 1 positive1: mov ax, di ; put the number in ax for division NextDigit: xor dx, dx ; ax = ax / 10, dx = ax % 10 div Ten cmp ax, 0 ; if ax==0 break je OutNextDigit add dl, '0' ; else store dl+'0' to NumStr mov NumStr[si], dl dec si ; jmp NextDigit OutNextDigit: add dl, '0' mov NumStr[si], dl lea dx, NumStr[si] ; print number mov ah, 09H int 21H add bx, 2 ; next iteration loop print_num ;====================ending work========================== pop bp pop ds mov ax, 4c00H int 21H main endp _CODE ends end main

 

你可能感兴趣的:(assembly,sort)