简体   繁体   English

x86汇编转换基数

[英]x86 Assembly Convert Base Numbers

Follow-up to a prior question. 后续问题的跟进。 I am trying to write two procedures in x86. 我正在尝试在x86中编写两个过程。 One procedure reads in an integer (ReadInteger) in a specific base and then one that writes it out (WriteInteger) in a different base. 一个过程在特定的基数中读取整数(ReadInteger),然后在另一个基数中将其写入(WriteInteger)。 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). 首先,ReadInteger可以接受任何基数的数字(例如1234,基数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. 然后,WriteInteger必须能够在eax中使用该整数,并在bl中使用新的基值,并将其转换为新的基数。 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? 我要询问的地方是否需要将ReadInteger过程或另一个过程中的所有内容转换为通用基数(例如十进制),然后将其转换,因为我只能在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 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. Base是人类用来使数字更易读的东西。 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.) (有些奇怪的例外,例如BCD,您可能最终会遇到,但不是今天。)

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. 因此,使用特定基数读取和使用特定基数写入的函数是完全独立的,并且它们之间唯一需要传递的信息是二进制数本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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