[英]Trying to store 32-bit number across 2 16-bit registers
这是针对 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
outputMsg: mov ah,9 ; load print function mov dx,msg ; load output message to print int 21h ; print output message
在outputMsg标签中, DX
保存了DX:BX
32 位数字的高位字。 通过编写mov dx, msg
你已经摧毁了它! 你需要保存它。
outputMsg:
push dx
mov ah, 09h
mov dx, msg
int 21h
pop dx
rol bx,1 ; rotate msb into CF
在32位数字的最高显著位(MSB) DX:BX
是位的15 DX
。
检索它的代码是:
shl bx, 1 ;Shift low word left, carry gets its highest bit
rcl dx, 1 ;Bring that carry in high word, carry gets msb
您当前输出“0”或“1”的解决方案有效,但更简洁的解决方案不使用jc
/ jmp
指令。 那些导致需要大量标签的杂乱代码!
将进位标志的值(0 或 1)添加到字符“0”将得到请求的“0”或“1”。
push dx
mov dl, '0'
adc dl, 0 ;The immediate is zero, so only the carry gets added!
mov ah, 02h
int 21h
pop dx
由于 DOS 输出函数使用DL
,因此需要保存和恢复DX
保存的数字的高位字。
当然,如果 32 位数字已存储在其他一些寄存器对(如DI:SI
或BP:BX
,则该程序的某些问题将不存在。 鉴于这是硬件, push
/ pop
有效。
作为最后的说明。
mov ah,04Ch ;DOS function: Exit program mov al,0 ;Return exit code value
当加载在 1 个字寄存器 ( AX
) 中组合的 2 字节寄存器( AH
和AL
)时,您可以通过组合加载来节省代码大小和执行速度:
mov ax, 4C00h ;DOS function: Exit program with exit code 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.