簡體   English   中英

嘗試跨 2 個 16 位寄存器存儲 32 位數字

[英]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:SIBP:BX ,則該程序的某些問題將不存在。 鑒於這是硬件, push / pop有效。


作為最后的說明。

 mov ah,04Ch ;DOS function: Exit program mov al,0 ;Return exit code value

當加載在 1 個字寄存器 ( AX ) 中組合的 2 字節寄存器( AHAL )時,您可以通過組合加載來節省代碼大小和執行速度:

mov  ax, 4C00h     ;DOS function: Exit program with exit code 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM