Hello i am trying to make a code that will transfer a decimal number in range of <0; 65 535> to binary, and so far my thought was to transfer the original String number to Number and then divide it by 2 in BX, so the result will be in AX and remainder in DX ... but thus far i do not know how to go further, I though maybe comparing if there is 0 or 1 in the remainder area which is DX after every divide and then writing it sing by sign in new String ?
TITLE MASM Vstup_výstup (main.asm)
INCLUDE Irvine32.inc
.data
Number DB 9 dup(?),0Dh,0Ah,§
Result DB 9 dup(?),0Dh,0Ah,§
.code
main PROC
call Clrscr
mov ebp, offset Number ;first number's adress is saved to ebp
mov esp, offset Result
mov ecx, 20 ;
mov edi, 1 ;
call ReadString
mov ax, Number ;
sub ax, '0'; ;String to Number by substracting 0
mov bx, 2 ;save 2 to 16 bit bx register
Start:
mov cl, [ebp+edi] ; something as a counter to know when to end with dividing ?
inc edi ; increase edi by one
cmp cl, § ; compare if the counter already came here division will jump to Ending
je Ending
div bx ; divide ax with bx, which means that result will be in AX and remainder in DX
cmp dx,0
je Zero ; if the remainder is 0 it will write 0 in the new string
jmp Start ; jmp to start
Zero:
mov esi, [esp+edi]
Ending:
exit
main ENDP
END main
It appears that what you're actually doing is changing a string which contains an ASCII representation of a decimal number into the equivalent number in a register. The typical way to do this is to start with the most significant digit and repeatedly multiply the previous result by ten and add the digit's value. In psuedo-code: