[英]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.