我正在尝试将十进制数转换为二进制数。我想将模数存储在数组中,然后打印结果。结果必须向后打印。到目前为止这是我的代码。运行时会显示一条消息unaligned address,exception 5
。这是我到目前为止的代码
.data
prompt1: .asciiz "\n\n Please give the decimal number:"
prompt2: .asciiz "The binary number is:"
array: .space 32
linefeed: .asciiz "\n"
enterkey: .asciiz "Press any key to end program."
.text
main:
li $t0,0
li $t3,2
li $s0, 0
li $a0, 0
#t1-to hold number
li $v0, 4 #syscall to print string
la $a0, prompt1 #address of string to print
syscall
li $v0, 5 #syscall to read an integer
syscall
move $t1, $v0 #move the number to read into $t1
for:
bge $s0, 8, end_for
div $t1,$t3
mflo $t1 #diversion
mfhi $t1 #modulus
sw $t1,array($t0) #save the number to read into array
addi $t0,$t0,4
addi $s0,$s0,1
j for
end_for:
# print out a line feed
li $v0,4 # code for print_string
la $a0,linefeed # point $a0 to linefeed string
syscall # print linefeed
li $v0,1
move $a0,$t0
syscall
# print out a line feed
li $v0,4 # code for print_string
la $a0,linefeed # point $a0 to linefeed string
syscall # print linefeed
# wait for the enter key to be pressed to end program
li $v0,4 # code for print_string
la $a0,enterkey # point $a0 to enterkey string
syscall # print enterkey
# wait for input by getting an integer from the user (integer is ignored)
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0
# All done, thank you!
li $v0,10 # code for exit
syscall # exit program
答案 0 :(得分:0)
我做了一些改进但仍然没有。这次我想 它不存储数组中的值
。数据
prompt1:.asciiz“\ n \ n请给出小数:”
prompt2:.asciiz“二进制数是:”
数组:.align 2
.space 32
换行:.asciiz“\ n”
enterkey:.asciiz“按任意键结束程序。”
的.text
主:
li $ t0,0
li $ t3,2
li $ s0,0
li $ a0,0
li $ v0,4 #syscall打印字符串
la $ a0,prompt1 #address of string
系统调用
li $ v0,5 #syscall读取整数
系统调用
移动$ t1,$ v0#移动要读入$ t1的数字
for:
bge $ s0,8,end_for
div $ t1,$ t3
mflo $ t2 #div
mfhi $ t4 #mod
移动$ t1,$ t2
li $ v0,1#print_int的代码
移动$ a0,$ t4#将结果移入$ a0
syscall#打印出结果
sw $ t4,array($ t0)#save要读入数组的数字
addi $ t0,$ t0,4
addi $ s0,$ s0,1
j for
END_FOR:
forprint:
bge $ s0,8,end_forprint
lw $ t4,array($ t0)#save要读入数组的数字
li $ v0,1#print_int的代码
移动$ a0,$ t4#将结果移入$ a0
syscall#打印出结果
addi $ t0,$ t0,4
addi $ s0,$ s0,1
j forprint
end_forprint:
li $ v0,4#print_string的代码
la $ a0,linefeed#point $ a0 to linefeed string
系统调用#print linefeed
li $ v0,4#print_string的代码
la $ a0,enterkey#point $ a0 to enterkey string
系统调用#print enterkey
li $ v0,5#read_int的代码
来自用户的系统调用#get int - >以$ v0
返回li $ v0,10#退出代码
系统调用#export程序