I am trying to convert a decimal number to a binary number. I want to store the modulus in an array and then print the result. The result must be printed backwards. This is my code so far. When it runs, a message appears saying unaligned address,exception 5
. Here is my code so far
.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
i made some improvements but still nothing.This time i think that it doesn't store the values in the array
.data
prompt1: .asciiz "\\n\\n Please give the decimal number:"
prompt2: .asciiz "The binary number is:"
array:.align 2
.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
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 $t2 #div
mfhi $t4 #mod
move $t1,$t2
li $v0,1 # code for print_int
move $a0,$t4 # put result in $a0
syscall # print out the result
sw $t4,array($t0) #save the number to read into array
addi $t0,$t0,4
addi $s0,$s0,1
j for
end_for:
forprint:
bge $s0, 8, end_forprint
lw $t4,array($t0) #save the number to read into array
li $v0,1 # code for print_int
move $a0,$t4 # put result in $a0
syscall # print out the result
addi $t0,$t0,4
addi $s0,$s0,1
j forprint
end_forprint:
li $v0,4 # code for print_string
la $a0,linefeed # point $a0 to linefeed string
syscall # print linefeed
li $v0,4 # code for print_string
la $a0,enterkey # point $a0 to enterkey string
syscall # print enterkey
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0
li $v0,10 # code for exit
syscall # exit program
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.