Yesterday, I saw someone asking about the Bubble sorting, and I wanted to write one. I haven't written a compilation for a long time. Recently I want to learn it well. I also made a lot of preparations and downloaded some compilers.
But after I write it out, I don't know how to compile it, because I used to learn dos assembly, which is different from the mainstream 32-bit. Later, although masm32 (with link16) was used to compile and link successfully and run in dosbox, but no suitable debugging tool was found, the 16-bit debugger also said that the format was not matched. Then I finally downloaded a set of 16-bit tools (masm, link, debug.
Maybe this old thing is not very useful to everyone, but I think that since I want to learn it well, I should start from the beginning, check the instruction manual and write it in the middle, and find that there are still many restrictions, for example, the distance between loops cannot be too long, and ax is used for multiplication and division. There are not so many restrictions.
This is the first step for me to start compilation again.
_ DATA segment <br/> NumArray DW 100 dup (?) <Br/> NumStrDB5dup (?), '', '$' <Br/> Msg1DB 'Please input array of numbers you want to sort (less than 100): ', 0dH, 0aH, '$' <br/> Msg2DB 'sorted Array: ', 0dH, 0aH, '$' <br/> TenDW10 <br/> Minus1DW-1 <br/> _ DATA ends </p> <p> _ CODE segment <br/> assume cs: _ CODE, ds: _ DATA </p> <p> main proc <br/>; ======================= initialization ============================ ==========< br/> pushds <br/> pushbp </p> <p> movax, _ DATA <br/> movds, ax; put _ DATA into ds </p> <p> leadx, Msg1 <br/> movah, 9; display Msg1 <br/> int21H <br/> xorcx, cx; store number <br/> xorbx, bx; store index <br/> xordx, dx; use for multiply <br/> xorbp, bp; store flag </p> <p>; ================ scanf characters from console =================================== ==< br/> input_num: <br/> movah, 1; read a char from console ==> al <br/> int21H </p> <p> cmpal, 0dH; al = return <br/> jelast_input <br/> cmpal, ''; al = space <br/> jestore_num <br/> cmpal ,'-'; al = minus <br/> jenegative <br/> cmpal, '+'; al = positive sign <br/> jeinput_num </p> <p> orbp, 01 H; al belongs [0-9], set bit 0 of bp <br/> subal, '0'; change char to value <br/> xorah, ah; ah = 0 <br/> xchgax, cx; cx = cx * 10 + ax <br/> imulTen <br/> addcx, ax <br/> jmpinput_num </p> <p> store_num: <br/> testbp, 01 H; bp and 1 = 1 means there is a num before space <br/> jzfake_input; if there's no number before space <br/> testbp, 02 H; bp and 2 = 2 means the number is negative <br/> jzpositive; if not negative <br/> notcx; else multiply-1 <br/> addcx, 1 <br/> positive: <br/> movNumArray [bx], cx; store cx to memory <br/> addbx, 2; each element take 2 bytes <br/> xorcx, cx <br/> fake_input: <br/> testbp, 04 H; bp and 4 = 4 means there's a return, so end input <br/> jnzbubble_sort <br/> xorbp, bp; clear bp for next input <br/> jmpinput_num </p> <p> negative: <br/> orbp, 02 H; set bit 1 of bp <br/> jmpinput_num </p> <p> last_input: <br/> orbp, 04 H; set bit 2 of bp <br/> jmpstore_num </p> <p>; =================== bubble sort ============================== ================< br/> bubble_sort: <br/> movcx, bx; store end position of array <br/> xorbx, bx; clear bx, bx is outer iterator <br/> xorbp, bp; clearbp, bp is inner iterator </p> <p> Loop1: <br/> cmpbx, cx; outer loop compare <br/> jnlOutLoop1 </p> <p> movdx, numArray [bx]; read to dx <br/> movbp, bx <br/> movax, bx </p> <p> Loop2: <br/> addbp, 2 <br/> cmpbp, cx; inner loop compare <br/> jnlOutLoop2 <br/> cmpdx, NumArray [bp]; find a minimum value after NumArray [bx] <br/> jngLoop2; record its position to ax <br/> movdx, NumArray [bp] <br/> movax, bp <br/> jmpLoop2 <br/> OutLoop2: <br/> cmpax, bx <br/> jeNoChange <br/> xchgdx, NumArray [bx]; exchange the values <br/> xchgax, bx <br/> xchgdx, NumArray [bx] <br/> xchgax, bx </p> <p> NoChange: <br/> addbx, 2 <br/> jmpLoop1 </p> <p> OutLoop1: </p> <p>; ========================== print the result ============================== ==========</p> <p> leadx, msg2; show Msg2 <br/> movah, 09 H <br/> int21H </p> <p> shrcx, 1; because the array element take 2 bytes, so cx/2 is the real count of array <br/> xorbx, bx; clear bx </p> <p> print_num: <br/> movsi, 4; there's 5 byte to store num, assume the number not exceed 32767 <br/> movdi, NumArray [bx]; read a element to di <br/> cmpdi, 0; if di <0 then print a'-', and di =-di <br/> jgepositive1 <br/> movdl,'-'<br/> movah, 02 H <br/> int21H <br/> notdi <br/> adddi, 1 <br/> positive1: <br/> movax, di; put the number in ax for division </p> <p> NextDigit: <br/> xordx, dx; ax = ax/10, dx = ax % 10 <br/> divTen <br/> cmpax, 0; if ax = 0 break <br/> jeOutNextDigit <br/> adddl, '0 '; else store dl + '0' to NumStr <br/> movNumStr [si], dl <br/> decsi; <br/> jmpNextDigit <br/> OutNextDigit: <br/> adddl, '0' <br/> movNumStr [si], dl </p> <p> leadx, NumStr [si]; print number <br/> movah, 09 H <br/> int21H <br/> addbx, 2; next iteration <br/> loopprint_num </p> <p>; ================================ ending work ========================== ========</p> <p> popbp <br/> popd.</p> <p> movax, 4c00H <br/> int21H <br/> main endp </p> <p> _ CODE ends <br/> end main