我在使用NASM汇编实现十进制转二进制、八进制和十六进制的转换时遇到了一些问题。以下是我已经完成的大部分代码,我知道它存在一些问题,正在努力修复中。我已经在代码中添加了相关信息的注释。
%include "io.mac"
.STACK 100H
.DATA
msg1 db "Please enter a positive integer (max 16 bits): ",0
msg2 db "The binary (base-2) representation of the number is: ",0
msg3 db "The octal (base-8) representation of the number is: ",0
msg4 db "The hex (base-16) representation of the number is: ",0
msg5 db "A",0 ;These will be implemented when I get the
msg6 db "B",0 ;hexadecimal loop working to change 10, 11,
msg7 db "C",0 ;etc. to their hex equivalents.
msg8 db "D",0
msg9 db "E",0
msg10 db "F",0
.UDATA
num1 resw 1
quotient resw 1
remainder resw 1
.CODE
.STARTUP
PutStr msg1
nwln
GetInt [num1]
nwln
PutStr msg2
nwln
mov AX, [num1]
sub BH, BH
binary_loop:
push AX
mov BH, byte 2
div BH ;All loops output the remainders in reverse order at the moment.
mov [remainder], AH ;I was thinking of maybe moving the remainder value
PutInt [remainder] ;to a stack then calling it at the end of the loop,
mov [quotient], AL ;when the quotient value is zero. However,
mov AX, [quotient] ;I do not know enough to make that work. Any help
cmp [quotient], byte 0 ;would be greatly appreciated
jz binary_done ;Could also be je, as both do the same thing
loop binary_loop
binary_done:
nwln
PutStr msg3
nwln
mov AX, [num1]
sub BH, BH
octal_loop:
push AX
mov BH, byte 8
div BH
mov [remainder], AH
PutInt [remainder]
mov [quotient], AL
mov AX, [quotient]
cmp [quotient], byte 0
jz octal_done
loop octal_loop
octal_done:
nwln
PutStr msg4
nwln
mov AX, [num1]
sub BH, BH
hex_loop:
push AX
mov BH, byte 16
div BH
mov [remainder], AH
PutInt [remainder]
mov [quotient], AL
mov AX, [quotient]
cmp [quotient], byte 0
jz done
loop hex_loop
done:
.EXIT
对于一个16位的测试数字155,当前的输出结果为: 二进制:11011001 八进制:332 十六进制:119