【发布时间】:2015-04-27 01:03:59
【问题描述】:
我在使用 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
【问题讨论】: