我需要帮助我的汇编程序中的第一个程序。 我必须将用户输入的值从十进制转换为二进制。 我不知道如何将值显示为小数,我接下来该怎么做。 任何人都可以一步一步地指导下一步做什么。
.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
答案 0 :(得分:1)
我认为你会好的。
; 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
也可以工作而不是adc eax,0
,并且在某些CPU上效率更高。
答案 1 :(得分:0)
不完全清楚你要做什么。我猜“十进制到二进制”,但提示“输入二进制值”。我认为这意味着一串“1”和“0”。我不会问他们的“小数”值 - 你会得到像“1.23”,你没有能力处理。只要问他们一个号码。也许“数字小于65536”,这可能是(?)你想要的。
警告!未经测试的代码!
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
现在我想你要打印那个数字的二进制(“1”和“0”)表示......
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 ; ?
自从我做了DOS之后已经有一段时间了,所以可能会有严重的错误,但它可能会给你一些想法。