簡體   English   中英

x86匯編轉換基數

[英]x86 Assembly Convert Base Numbers

后續問題的跟進。 我正在嘗試在x86中編寫兩個過程。 一個過程在特定的基數中讀取整數(ReadInteger),然后在另一個基數中將其寫入(WriteInteger)。 我所苦苦掙扎的地方比實際的代碼與解決方案更相關。

首先,ReadInteger可以接受任何基數的數字(例如1234,基數5)。 然后,WriteInteger必須能夠在eax中使用該整數,並在bl中使用新的基值,並將其轉換為新的基數。 我要詢問的地方是否需要將ReadInteger過程或另一個過程中的所有內容轉換為通用基數(例如十進制),然后將其轉換,因為我只能在WriteInteger中接受整數和新的基值? 我還有其他想念的方式嗎? 我似乎沒有其他辦法可以做到,但是作業看起來應該比這更簡單。

到目前為止,這是我的代碼。

;-----------------------------------------------------
ReadInteger PROC
;
; ReadInteger is passed one argument in bl representing the base of the number to be input. 
; Receives: bl register (original base)
; Returns:  EAX
;-----------------------------------------------------
nextChar:
     mov edx, 0             ; prepare for divide
     mov base, ebx
     call ReadChar          ; Get the next keypress
     call WriteChar         ; repeat keypress
     call AsciiToDigit      
     div base
     shl   ebx,1            ; shift to make room for new bit
     or    ebx,base         ; set the bit to eax
     cmp al, 13             ; check for enter key
     jne   nextChar
     mov eax, ebx           ; place integer value in eax for return
     ret
ReadInteger ENDP

;-----------------------------------------------------
WriteInteger PROC
;
; Will display a value in a specified base
; Receives: EAX register (integer), bl (new base)
; Returns:  nothing
;-----------------------------------------------------

     mov   ecx, 0         ; count the digits
nextDigit:
     mov   edx, 0         ; prepare unsigned for divide
     div   ebx
     push  edx            ; remainder will be in dl
     inc   ecx            ; count it!
     cmp   eax, 0         ; done when eax becomes 0
     jne   nextDigit

                            ; pop them off and convert to ASCII for output
outDigit: 
     pop   eax              ; digits come off left to right
     add   eax, '0'         ; add 0 to get ASCII
     call  WriteChar        
     loop  outDigit         

     call Crlf
     ret


ret

WriteInteger ENDP

寄存器中的數字沒有特定的“基數”,它們只是二進制數。 Base是人類用來使數字更易讀的東西。 這意味着“基本”的概念僅適用於輸入和輸出,而機器內部則沒有。 (有些奇怪的例外,例如BCD,您可能最終會遇到,但不是今天。)

因此,使用特定基數讀取和使用特定基數寫入的函數是完全獨立的,並且它們之間唯一需要傳遞的信息是二進制數本身。

暫無
暫無

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

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