【发布时间】:2018-03-03 02:42:50
【问题描述】:
这是针对 8086 的,我使用的是 NASM。
我有一直在努力解决的硬件问题,我应该接受一个 32 位二进制数作为输入,将其存储在寄存器对 dx:bx 中,然后将数字输出到屏幕上。
我有一个问题我很久没能解决了。它不是输出输入的 32 位,而是输出输入的最后 16 位,并执行两次。
有人可以看看这个并帮助我理解为什么我没有得到完整的 32 位数字吗?
CPU 286
org 100h
section .data
prompt: db "Please enter a binary number: $"
msg: db 0Dh,0Ah, "The number you entered is: $"
section .text
start:
mov ah,9 ; load print function
mov dx,prompt ; load prompt to print
int 21h ; prompt for input
mov bx,0 ; bx holds input value
mov dx,0 ; clear dx
mov ah,1 ; input char function
int 21h ; read char into al
; loop to accept and store input
input:
cmp al,0Dh ; is char = CR?
je outputMsg ; yes? finished with input
shl bx,1 ; bx *= 2, shifts msb of BX into CF
rcl dx,1 ; rotate CF into lsb of DX
and al,01h ; converts ASCII to binary value
or bl,al ; "adds" the input bit
int 21h ; read next character
jmp input ; loop until done
outputMsg:
mov ah,9 ; load print function
mov dx,msg ; load output message to print
int 21h ; print output message
; loop to load either '0' or '1' to print, depending on carry flag
mov cx, 32 ; loop counter
output:
rol bx,1 ; rotate msb into CF
jc one ; if CF is 1, go to "one" loop
mov dl,'0' ; of CF is 0, set up to print '0'
jmp print ; jump to "print" loop
one:
mov dl,'1' ; set up to print '1'
print:
mov ah,2 ; load print char fcn
int 21h ; print char
loop output ; go to next character
Exit:
mov ah,04Ch ;DOS function: Exit program
mov al,0 ;Return exit code value
int 21h ;Call DOS. Terminate program
【问题讨论】:
-
因为您在 int 21h 调用中使用它完全丢弃了 dx 中的高位,并且您的循环仅循环 bx 并且根本不关心其中的 dx。您必须实际使用该值来打印它。
-
@SamiKuhmonen 谢谢!多亏了你的建议,我终于让它工作了。