问题
I need help with my first program in assembler. I have to convert values entered by user from decimal to binary. I have no idea how can I show values as a decimal, and what should I do next. could anyone instruct me step by step what do next.
.model small
.stack 100h`
.data
txt1 db "Enter binary value:" ,10,13, "$"
txt2 db "BIN: " ,10,13, "$"
.code
main proc
mov ax, @data
mov ds, ax
;clear screen
mov ah,0fh
int 10h
mov ah,0
int 10h
;show first text
mov ah, 9
mov dx, offset txt1
int 21h
call Number
main endp
Number proc
mov cx,5
xor bx,bx
read:
mov ah,0
int 16h
cmp al,'0'
jb read
cmp al, '9'
ja read
mov ah,0eh
int 10h
loop read
Number endp
mov ax, 4c00h
int 21h
end main
回答1:
I think it will be ok for you.
; Read an integer from the screen and display the int in binary format
; and continue until number is negative.
again: ; For loop
call read_int ; take the integer from screen
cmp eax,0 ; look if number is not negative
JL end: ; if less than zero program ends.
mov ecx,32 ; for loop we set ecx to 32 ; ATTENTION we not specified type. So compiler will get error.
mov ebx,eax ; we will lost our number in eax, so I take it to ebx
START:
xor eax,eax ; eax = 0
SHL ebx,1 ; shift the top bit out of EBX into CF
ADC eax,0 ; EAX = EAX + CF + 0 ADD CARRY FLAG, so eax is zero we add zero. The new eax will exact value of Carry Flag which is out bit.
call print_int ; Then we print the CF which we took the eax.
LOOP start: ; Loop looks ecx if not 0 it goes start.
call print_nl ; For next number we print a new line
JMP again: ; For take new number
END: ; End of the program.
setc al
would also work instead of adc eax,0
, and be more efficient on some CPUs.
回答2:
Not entirely clear what you're trying to do. I guess "decimal to binary", but the prompt says "Enter binary value". I would take that to mean a string of "1"s and "0"s. I wouldn't ask 'em for a "decimal" value either - you'll get like "1.23", which you aren't equipped to handle. Just ask 'em for a number. Maybe "a number less than 65536", which is probably(?) what you want.
Warning! Untested code ahead!
Number proc
mov cx,5 ; loop counter?
xor bx,bx ; "result so far"?
read:
mov ah,0
int 16h
; wanna give 'em the option to enter
; less than the full five digits?
cmp al, 13 ; carriage return
jz finis
cmp al,'0'
jb read
cmp al, '9'
ja read
mov ah,0eh
int 10h
; Assuming al still holds your character...
sub al, '0' ; convert character to number
mov ah, 0 ; make sure upper byte is clear
imul bx, bx, 10 ; multiply "result so far" by 10
; jc overflow ; ignore for now
add bx, ax ; add in the new digit
; jc overflow ; ignore for now
loop read
finis:
; now our number is in bx
; it is conventional to return values in ax
mov ax, bx
overflow: ; I'm just going to ignore it
; spank the user?
; go right to exit?
ret ; maybe endp generates this. two shouldn't hurt
Number endp
Now I guess you want to print the binary ("1"s and "0"s) representation of that number...
Printbin proc
; what does "proc" do? Do you know?
mov bx, ax
mov cx, 16 ; 16 bits to do, right?
mov ah, 2
top:
mov dl, '0'
shl bx, 1 ; shift leftmost bit to carry flag
adc dl, 0 ; bump the "0" up to "1", if set
int 21h
loop top
ret
endp ; ?
Been a while since I've done DOS, so there may be serious errors in that, but it may give you some ideas.
来源:https://stackoverflow.com/questions/27336052/converting-decimal-to-binary-in-assembler