简体   繁体   中英

x86 Assembly Convert Base Numbers

Follow-up to a prior question. I am trying to write two procedures in x86. One procedure reads in an integer (ReadInteger) in a specific base and then one that writes it out (WriteInteger) in a different base. Where I'm struggling is more related to the solution than the actual code.

First, ReadInteger can take in a number from any base (example 1234, base 5). Then WriteInteger must be able to take that integer in eax and a new base value in bl and convert it to the new base. Where I'm questioning is do I need to convert everything in the ReadInteger procedure or in another procedure to a common base (say decimal) then convert it since I can only take in the integer and the new base values in WriteInteger? Is there another way I'm missing? I can't seem to think of any other way to do it but the assignment reads like it should be simpler than this.

Here is my code so far.

;-----------------------------------------------------
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

Numbers in registers don't have a specific "base", they're just binary numbers. Base is something that humans use to make numbers more readable. This means that the concept of "base" only applies to input and output, nothing internal to the machine. (There are weird exceptions such as BCD that you might run across eventually, but not today.)

So, your functions that read with a particular base, and write with a particular base are completely independent and the only information that need pass between them is the binary number itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM