So I have made a program which allows users to put how many hello world that they want and after ## there is another function but that won't play out instead the program just closes.
.data
n: .space 4
msg: .asciiz "Hello World"
prom1: .asciiz "How many Hello World want to be printed: "
mychar1:.byte 'a'
out_string: .asciiz "\nHello World\n"
prom: .asciiz "Type a number: "
mychar: .byte 'm'
res: .asciiz "Result is: "
nl: .asciiz "\n"
.text
main: li $v0, 4
la $a0, msg
syscall
li $v0, 4 # print str
la $a0, nl # at nl
syscall
li $v0, 4 # print str
la $a0, nl # at nl
syscall
li $v0, 4
la $a0, prom1 # Load address of first prompt
syscall
li $v0, 5 # Read int from user
syscall
li $t1, 0 # Load 0 into $t1 for comparison
move $t0, $v0 # Move the user input to $t0
loop:
beq $t1, $t0, end # Break If Equal: branch to 'end' when $t1 == $t2
li $v0, 4
la $a0, out_string # Load address of output string
syscall
add $t1, $t1, 1 # Increment $t1
j loop # Jump back up to loop
end:
li $v0, 10 # Load syscall 10 to indicate end of program
syscall
##
li $v0, 4 # print str
la $a0, nl # at nl
syscall
li $v0, 4 # print str
la $a0, prom # at prom
syscall
li $v0, 5 # read int
syscall
sw $v0, n # store in n
li $v0, 4 # print str
la $a0, res # at res
syscall
li $v0, 1 # print int
lw $t0, n # n
sub $t1, $t0, 1 # n-1
mul $t0, $t0, $t1 # *
sra $a0, $t0, 1 # /2
syscall
li $v0, 4 # print str
la $a0, nl # at nl
syscall
li $v0, 4 # print str
la $a0, nl # at nl
syscall
li $v0, 10 # exit
syscall
In this spot here
end:
li $v0, 10 # Load syscall 10 to indicate end of program
syscall
You are calling a syscall with 10 in the $v0 register.
This tells the computer to stop the program. So in this case you are just stopping the program before the rest of the code is executed.
Instead you could do this
loop:
beq $t1, $t0, endloop
li $v0, 4
la $a0, out_string # Load address of output string
syscall
add $t1, $t1, 1 # Increment $t1
j loop # Jump back up to loop
endloop:
# Put rest of code here
This is just the way to end the loop and continue.
Then at the end of the program you can put the syscall 10
Related
I am trying to get the output in the same line with the result(string). I have found examples, but none of them explained the process. I know that the string has to be store in memory and then access it through bit by bit, but I got lost in the process.
.data
prompt: .asciiz "Enter your name: "
name: .space 101
result: .asciiz " ...that's the name"
.text
.globl main
main:
la $a0, prompt
li $v0, 4
syscall
la $a0, name # Get the input
li $v0, 8
li $a1,101
syscall
la $a0, name # print the result
li $v0, 4
syscall
la $a0, result
li $v0, 4
syscall
li $v0,10
syscall
The problem comes from that, when you read in the input the new line will be read also, so it has to be removed in order to achieve your needs.
This is one way to do it:
.data
prompt: .asciiz "Enter your name: "
name: .space 101
result: .asciiz " ...that's the name"
.text
.globl main
main:
la $a0, prompt
li $v0, 4
syscall
la $a0, name # Get the input
li $v0, 8
li $a1,101
syscall
addi $t1, $t1, 0 # len = 0
len_to_new_line:
lb $t2, ($a0) # t2 = *a0
beq $t2, '\n', end # if t2 == '\n' -> stop
addi $t1, $t1, 1 # len++
addi $a0, $a0, 1 # a0++
b len_to_new_line
end:
la $a0, name
add $a0, $a0, $t1
sb $zero, ($a0) # overwrite '\n' with 0
la $a0, name # print the result
li $v0, 4
syscall
la $a0, result
li $v0, 4
syscall
li $v0,10
syscall
Output
Enter your name: David
David ...that's the name
EDIT: The len is not even needed and code can be reduced to this (only the changed part)
len_to_new_line:
lb $t2, ($a0) # t2 = *a0
beq $t2, '\n', end # if t2 == '\n' -> stop
addi $a0, $a0, 1 # a0++
b len_to_new_line
end:
sb $zero, ($a0) # overwrite '\n' with 0
I'm trying to finish up this MIPS calculator, super basic, my first mips program. It doesn't have to handle overflow or anything like that, just has to work on small, positive numbers.
I've not checked my algorithms for multiply and divide, because I am just trying to get add working.
I cannot for the life of me figure out why the ints will not read in and also I'm getting a memory out of bounds when I call lb $a0, op to display the operator for output and don't understand why.
I'm new to this so anything is probably helpful. Thanks in advance.
.data
# const string for welcome
welc: .asciiz "Welcome to SPIM Calculator 1.0!\n"
p_int: .asciiz "\nPlease give an integer: "
p_op: .asciiz "\nPlease give an operator: "
i_err: .asciiz "\nInput Incorrect, bad operator!\n"
again_str: .asciiz "Another calculation? (y/n)"
rmndr: .asciiz " r: "
new_line: .asciiz "\n"
int1: .word 1 # space to hold int 1
int2: .word 1 # space to hold int 2
raw_in: .space 1 # space to hold raw input
op: .space 1 # space to hold operator char
a_char: .space 1 # space to hold again char
out: .word 1 # space to hold output
remain: .word 1 # space to hold remainder
#operator constants
c_plus: .ascii "+" # const for +
c_min: .asciiz "-" # const for -
c_mult: .asciiz "*" # const for *
c_divi: .asciiz "/" # const for /
c_eq: .asciiz "=" # const for =
c_no: .asciiz "n" # const for n
.text
.globl main
main: li $v0, 4 # syscall 4, print string
la $a0, welc # give argument: string
syscall # actually print string
calc: la $t6, remain # load remainder variable
move $t6, $zero # store 0 in remainder (reset)
li $v0, 4 # syscall 4, print string
la $a0, p_int # give argument: string
syscall # actually print string
li $v0, 5 # tell syscall we want to read int 1
syscall # actually read in int 1
la $s1, int1 # load int1 into $s1
move $s1, $v0 # copy the integer from $v0 to int1
li $v0, 4 # syscall 4, print string
la $a0, p_int # give argument: string
syscall # actually print string
li $v0, 5 # tell syscall we want to read int 2
syscall # actually read in int 2
la $s2, int2 # give $s2 the address to hold int 2
move $s2, $v0 # copy the integer from $v0 to $s2
li $v0, 4 # syscall 4, print string
la $a0, p_op # give argument: string
syscall # actually print string
li $v0, 8 # tell syscall we want to read operator
la $a0, op # give $a0 the address to hold the operator
syscall # actually read in operator
lb $t0, op # load the first byte of op
li $t1, '+' # load const for plus
li $t2, '-' # load const for minus
li $t3, '*' # load const for multiplying
li $t4, '/' # load const for dividing
la $s0, out # load out to $s0
beq $t0, $t1, plus # we're adding
beq $t0, $t2, minus # we're subtracting
beq $t0, $t3, multi # we're multiplying
beq $t0, $t4, divi # we're dividing
# else
j error # incorrect input
plus: add $s0, $s1, $s2 # add our ints, store in $t0
j print
minus: sub $s0, $s1, $s2 # subtract our ints, store in $t0
j print
multi: slt $t1, $t2, $s2 # if our counter is less than int2, set $t1 to 1
beq $t1, $zero, print # if we've reached int2, we're done
add $s0, $s1, $s1 # add int1 and int1, store in out
j multi # loop
divi: la $t0 remain # load remainder into $t0
move $t0, $s1 # set remainder to dividend
add $s0, $zero, $zero # set out to 0, just in case
loop: slt $t1, $t0, $s2 # if remainder is less than divisor, set 1
beq $t1, $zero, print # if we're done branch to done
sub $t0, $t0, $s2 # sub divisor from remainder, store in remainder
addi $s0, $s0, 1 # increment quotient by 1
j loop # loop
print: li $v0, 1 # tell syscall we want to print int
la $a0, int1 # give syscall int1 to print
syscall # actually print int
li $v0, 4 # tell syscall we want to print string
lb $a0, op # tell syscall we want to print operator
syscall # actually print string
li $v0, 1 # tell syscall we want to print int
la $a0, int2 # give syscall int2 to print
syscall # actually print int
li $v0, 4 # tell syscall we want to print string
la $a0, c_eq # tell syscall we want to print operator
syscall # actually print string
li $v0, 1 # tell syscall we want to print integer
la $a0, out # give syscall our output
syscall # actually print int
la $t0, remain # load remainder
beq $t0, $zero, again # if we have no remainder, finish printing
li $v0, 4 # tell syscall we want to print string
la $a0, rmndr # tell syscall we want to print remainder string
syscall # print "r: "
li $v0, 1 # tell syscall we want to print int
la $a0, remain # give syscall our remainder to print
syscall # print remainder
again: li $v0, 4 # tell syscall we want to print string
la $a0, new_line # tell syscall to print new line
syscall
la $a0, again_str # load prompt for again string for syscall
syscall
li $v0, 8 # tell syscall we want to read string
la $a0, a_char # tell syscall to put it in $a0
syscall
lb $t0, a_char
li $t1, 'n' # get n char so we can compare
beq $t0, $t1, exit # if we are done, exit
#else loop
j calc # jump to beginning
error: li $v0, 4 # tell syscall we want to print string
la $a0, i_err # give syscall what to print
syscall # actually print
j again # go to prompt for retry
exit: li $v0, 10 # exit code
syscall #exit!
screenshot
The problem is that you don't use the appropriate instruction to handle memory.
Instead of move you should use sw (store word). This code will not store the int into int1:
la $s1, int1 # load int1 into $s1
move $s1, $v0 # copy the integer from $v0 to int1
instead, you should write:
la $s1, int1 # load address of int1 into $s1
sw $v0, 0($s1) # copy the integer from $v0 to int1
Like storing, loading from memory require two instructions:
la $s1, p_op # or whatever register you choose to use
lb $a0, 0($s1) # load byte from the address stored in $s0 (in index 0)
if you want to load the address of p_op into $a0, you should use la $a0, p_op, not lb
I'm writing a MIPS program (assembly language) that takes in 10 integers and prints out a histogram represented by asterisks.
E.g.:
User input of 1, 2, 3, 4
Output:
*
**
***
****
I have most of this code written already in MIPS. The problem I am running into is printing out the correct length of asterisks. As of now it is simply printing out the a histogram all of the same length; the FIRST user inputed integer.
# program functionality:
.data
menu: .asciiz "\n1. New Histogram\n2. Print Histogram\n3. Quit\n"
prompt: .asciiz "\nEnter 10 numbers between 0 and 50 (inclusive):\n"
prompt1: .asciiz "\nEnter a valid number:\n"
asterisk: .asciiz "*"
space: .asciiz "\n"
array: .word 0:10
.text
main:
do:
jal print_menu
li $v0, 5
syscall
beq $v0, 1, new
beq $v0, 2, print
beq $v0, 3, quit
j do # end do
new:
jal new_user
j do
print:
jal print_user
j do
j quit
print_menu:
la $a0, menu
li $v0, 4
syscall
jr $ra
new_user:
la $a0, prompt
li $v0, 4
syscall
enter_loop:
la $t0, array
li $t1, 10
enter_loop_2:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($t0)
addi $t1, $t1, -1
beqz $t1, end_loop_2
addi $t0, $t0, 4
j enter_loop_2
end_loop_2:
jr $ra
print_user:
la $t0, array
li $t1, 10
pLoop:
la $a0, space
li $v0, 4
syscall
asterisk_fun:
li $v0, 1
lw $a0, ($t0)
syscall
counter:
la $a0, asterisk
li $v0, 4
syscall
addi $a0, $a0, -1
beqz $a0, asterisk_end
j counter
asterisk_end:
jr $ra
addi $t1, $t1, -1
beqz $t1, endpLoop
addi $t0, $t0, 4
j pLoop
endpLoop:
jr $ra
quit:
li $v0, 10
syscall
The problems is that you are overwriting register $a0 in counter with the address of the asterisk, and you also used $a0 to count the number of items in that bucket.
Easy solution is to use other register (e.g. $a1) to count the number of items:
That would be:
#... your code
asterisk_fun:
li $v0, 1
lw $a1, ($t0) # Load number in $a1
move $a0, $a1 # move to $a0 just to print it
syscall
la $a0, asterisk
counter:
li $v0, 4
syscall
addi $a1, $a1, -1 # we use $a1 to keep the counter
beqz $a1, asterisk_end
j counter
asterisk_end:
# ... more of your code
As our term project, we're implementing a binary search tree. The thought behind it is as follows:
Assume a bst with 3 nodes:
10
/ \
/ \
8 14
Its address representation is as follows (value, left node address, right node address, root node address)t:
400:|----------|
| 8 |
|----------|
| 0 |
|----------|
| 0 |
|----------|
| 620 |
|----------|
| . |
| . |
| . |
$a0=620:|----------|
| 10 |
|----------|
| 400 |
|----------|
| 1000 |
|----------|
| 0 |
|----------|
| . |
| . |
| . |
1000:|----------|
| 14 |
|----------|
| 0 |
|----------|
| 0 |
|----------|
| 620 |
|----------|
So, according to these information, how can I struct a bst in mips assembly? Integers are 4-bytes yes, but the other ones are addresses, so they need 4-bytes since mips addresses are 32 bits. So does every node worth 4+4+4+4=16 bytes?
I had a similar project.
My solution was as follows:
##################################################
#
# Binary Search Tree - Project 1
#
# First Assembly Program :)
#
##################################################
.data
nl: .asciiz "\n"
prompt1: "\nPlease select an option from the list below:\n"
prompt2: "[A] - Add a record to the tree\n"
prompt3: "[F] - Find a record in the tree\n"
prompt4: "[P] - Perform a preorder traversal\n"
prompt5: "[I] - Perform an inorder traversal\n"
prompt6: "[Q] - Quit the program\n"
prompt7: "\nChoose a character: "
empty: "\nThe Tree is Empty."
youentered: "\nYou Entered: "
recordfound: "\nRecord Found: "
recordnotfound: "\nRecord Not Found! "
goodbye: "\nGoodbye!"
addid: "\nEnter the ID to add: "
addyear: "Enter the year: "
addtitle: "Enter the title: "
adddescription: "Enter the description: "
id: "\nID: "
year: "\nYear: "
title: "\nTitle: "
description: "Description: "
#idsize: .word 0
#optiona: 97 addrecord a
#optionf: 102 findrecord f
#optionp: 112 preorder p
#optioni: 105 inorder i
#optionq: 113 quit q
###################################################################
#
# Note: I reuse a lot of print statements
# This code is far from what I would call optimized
#
# This is my first assembly program and I'm really just
# Happy to see it all working :)
#
# I spent about 18 hours writing this so lol :)
#
# The only thing that I've gotten to crash it so far is
# Entering characters when it's waiting for an int :)
#
######################################################
#
# Here is my memory setup:
#
# $s5 - Stores Root Node
# $s7 - Stores Size of Tree (Not Really Necessary)
#
# Each New Item Contains a chunk of 344 bytes
# The bytes are setup as such:
#
# 8 Bytes - [ID]
# 8 Bytes - [Year]
# 64 Bytes - [Title]
# 256 Bytes - [Description]
# 8 Bytes - [LeftNodeAddress]
# 8 Bytes - [RightNodeAddress]
#
#
# Example Tree:
#
# 10 -Root
# / \
# 7 15 -Branch
# / \ / \
# 6 9 12 17 -Leaf
#
# In Memory:
#
# [Offset: 328] - [ID] - [Offset: 336]
# | |
# [Off: 328][ID][Off:336] [Off: 328][ID][Off: 336] . . .
#
#
########################################################
.text
###################
##Prompt Function##
###################
prompt:
li $v0, 4
la $a0, prompt1 #Please select an option from the list below:
syscall
li $v0, 4
la $a0, prompt2 #[A] - Add a record to the tree
syscall
li $v0, 4
la $a0, prompt3 #[F] - Find a record in the tree
syscall
li $v0, 4
la $a0, prompt4 #[P] - Preorder traversal
syscall
li $v0, 4
la $a0, prompt5 #[I] - Inorder traversal
syscall
li $v0, 4
la $a0, prompt6 #[Q] - Quit the program
syscall
###################
##Get User Input ##
###################
getinput:
li $v0, 4 #Choose a character:
la $a0, prompt7
syscall
li $v0, 12 #Read a single character from console
syscall
move $s0, $v0
beq $s0, 97, addrecord #If you press 'a', addrecord
beq $s0, 102, findrecord #If you press 'f', findrecord
beq $s0, 112, preorder #If you press 'p', preorder
beq $s0, 105, inorder #If you press 'i', inorder
beq $s0, 113, exit #If you press 'q', exit
li $v0, 4 #If you press something random
la $a0, nl #Display new line
syscall
j getinput #And ask for a new character
###################
## Add A Record ##
###################
addrecord:
li $v0, 9 #allocate memory for new record
li $a0, 344 #enough memory for 2 addresses and all the data
syscall
move $s0, $v0 #hang onto the initial address of all our info
li $v0, 4 #prompt for ID
la $a0, addid
syscall
li $v0, 5 #enter integer
syscall
sw $v0, 0($s0) #store our ID into memory Offset: 0
li $v0, 4 #prompt for add year
la $a0, addyear
syscall
li $v0, 5 #enter integer
syscall
sw $v0, 4($s0) #store year into our memory Offset: 4
li $v0, 4 #prompt for add title
la $a0, addtitle
syscall
li $v0, 8 #read our title into the allocated space
la $a0, 8($s0) #Offset: 8
li $a1, 64
syscall
li $v0, 4 #prompt for add description
la $a0, adddescription
syscall
li $v0, 8 #read our description into the allocated space
la $a0, 72($s0) #Offset: 72
li $a1, 256
syscall
bne $s7, 0, setlocations #if this isn't root node let's set the locations
add $s7, $s7, 1 #add 1 to the size of the records
move $s5, $s0 #store this address as root node for now
j prompt
########################
##Set Memory Locations##
########################
setlocations:
move $s6, $s5 #Keep $s5 as our root and use $s6 as temporary storage
move $s4, $s6 #Use $s4 to find the null node slot
storelocations:
beqz $s4, store #If we've reached a leaf, store
lw $t2, 0($s4) #get ID from current node
lw $t1, 0($s0) #get Current ID from new node node we're adding
ble $t1,$t2,goleft #get left location if new node <= current node
move $s6, $s4
lw $s4, 336($s4) #get node to the right if new node > current node
li $t3, 336 #be ready to store to the right slot
j storelocations
goleft:
move $s6, $s4
lw $s4, 328($s4) #load the node to the left
li $t3, 328 #be ready to store to the left slot
j storelocations
store:
beq $t3, 336, storeright #if $t3 was set to storeRight, then store to the right
sw $s0, 328($s6) #else store the new node's location into our node's left slot
add $s7, $s7, 1 #remind our size register that it's growing
j prompt #back to the prompt
storeright:
sw $s0, 336($s6) #store new node to the right slot
add $s7, $s7, 1 #remind our size register that it's growing
j prompt #back to the prompt
########################
## Find Record by ID ##
########################
findrecord:
move $s6, $s5
bne $s7, 0, search
li $v0, 4 #if tree is empty
la $a0, empty #display message Tree is empty
syscall
j prompt #and go wait for input
search:
move $s6, $s5
li $v0, 4 #print ID:
la $a0, id
syscall
li $v0, 5 #let user enter ID
syscall
move $t1, $v0 #store the id we're looking for in $t1
checkagain:
lw $t2, ($s6) #store the id we're currently looking at
bne $t1, $t2, checkempty #if this isn't the right ID, is it the last one?
###########################
##If we find the record:
###########################
li $v0, 4
la $a0, recordfound #Record Found:
syscall
li $v0, 4 #Print ID:
la $a0, id
syscall
li $v0, 1 #Print the ID stored at $s6 [Offset: 0]
lw $a0, 0($s6)
syscall
li $v0, 4 #Print Year:
la $a0, year
syscall
li $v0, 1 #Print the Year stored at $s6 [Offset: 4]
lw $a0, 4($s6)
syscall
li $v0, 4 #Print Title:
la $a0, title
syscall
li $v0, 4 #Print the Title stored at $s6 [Offset: 8]
la $a0, 8($s6)
syscall
li $v0, 4 #Print Description:
la $a0, description
syscall
li $v0, 4 #Print descript stored at $s6 [Offset: 72]
la $a0, 72($s6)
syscall
j getinput
checkempty:
ble $t1, $t2, checkleft #If $t1 <= $t2 check the left node
lw $s6, 336($s6) #Otherwise check the right node
bne $s6, 0, checkagain #If this record isn't empty, check again
li $v0, 4 #Otherwise
la $a0, recordnotfound #Record not found
syscall
j getinput
checkleft:
lw $s6, 328($s6) #Check the left node
bne $s6, 0, checkagain #If the record isn't empty, check again
li $v0, 4 #Otherwise
la $a0, recordnotfound #Record not found
syscall
j getinput
treeempty:
li $v0, 4 #if tree is empty
la $a0, empty #display message Tree is empty
syscall
j prompt
#####################################
#
# The Inorder Function
#
#####################################
inorder:
beq $s7, 0, treeempty #If the tree is empty display empty message
move $s6, $s5 #$s6 is the record we're currently at
move $t0, $s6 #t0 will iterate $s6 is our starting node
move $t1, $t0 #t1 will be thrown on the stack to keep track of everything
jal printinorder
j prompt
printinorder:
addi $sp, $sp, -12 #allocate 12 bytes for the stack
sw $ra, 0($sp) #4 for the $ra variable
sw $t1, 4($sp) #4 for $t1
bne $t0, 0, dontreturn #if $t0 isn't null don't return
lw $ra, 0($sp) #otherwise:
lw $t1, 4($sp) #pop stack
addi $sp, $sp, 12 #and prepare
jr $ra #to return
dontreturn:
move $t1, $t0 #put $t0 in $t1
lw $t0, 328($t0) #load the next pointer to the left
jal printinorder #and recurse back to printorder
move $s6, $t1 #if we're back here, it's time to print
j goprint #so go print
afterprint:
move $t0, $t1 #after we print, move $t1 back to $t0
lw $t0, 336($t0) #get the next pointer to the right
jal printinorder #recurse to see if it's the last one
move $s6, $t1 #if we made it here, it is, let's print
beq $s6, $t1, done #if we already printed this one, we're done (Root Node)
j goprint #Go print the node to the right
done:
lw $ra, 0($sp) #if we're done, pop our stack
lw $t1, 4($sp) #clean it up
addi $sp, $sp, 12 #12 bytes worth
jr $ra #and return
goprint:
li $v0, 4 #Print ID:
la $a0, id
syscall
li $v0, 1 #Print the ID stored at $s6 [Offset: 0]
lw $a0, 0($s6)
syscall
li $v0, 4 #Print Year:
la $a0, year
syscall
li $v0, 1 #Print the Year stored at $s6 [Offset: 4]
lw $a0, 4($s6)
syscall
li $v0, 4 #Print Title:
la $a0, title
syscall
li $v0, 4 #Print the Title stored at $s6 [Offset: 8]
la $a0, 8($s6)
syscall
li $v0, 4 #Print Description:
la $a0, description
syscall
li $v0, 4 #Print descript stored at $s6 [Offset: 72]
la $a0, 72($s6)
syscall
j afterprint
#####################################
#
# The Preorder Function
#
#####################################
preorder:
beq $s7, 0, treeempty #If the tree is empty display empty message
move $s6, $s5 #$s6 is the record we're currently at
move $t0, $s6 #t0 will iterate $s6 is our starting node
move $t1, $t0 #t1 will be thrown on the stack to keep track of everything
jal printpreorder
j prompt
printpreorder:
addi $sp, $sp, -12 #allocate 12 bytes for the stack
sw $ra, 0($sp) #4 for the $ra variable
sw $t1, 4($sp) #4 for $t1
bne $t0, 0, dontreturnpo #if $t0 isn't null don't return
lw $ra, 0($sp) #otherwise:
lw $t1, 4($sp) #pop stack
addi $sp, $sp, 12 #and prepare
jr $ra #to return
dontreturnpo:
move $s6, $t0 #if we made it here, it is, let's print
j goprintpo #so go print
afterprintpo:
move $t1, $t0 #put $t0 in $t1
lw $t0, 328($t0) #load the next pointer to the left
jal printpreorder #and recurse back to printorder
move $t0, $t1 #after we print, move $t1 back to $t0
lw $t0, 336($t0) #get the next pointer to the right
jal printpreorder #recurse to see if it's the last one
donepo:
lw $ra, 0($sp) #if we're done, pop our stack
lw $t1, 4($sp) #clean it up
addi $sp, $sp, 12 #12 bytes worth
jr $ra #and return
goprintpo:
li $v0, 4 #Print ID:
la $a0, id
syscall
li $v0, 1 #Print the ID stored at $s6 [Offset: 0]
lw $a0, 0($s6)
syscall
li $v0, 4 #Print Year:
la $a0, year
syscall
li $v0, 1 #Print the Year stored at $s6 [Offset: 4]
lw $a0, 4($s6)
syscall
li $v0, 4 #Print Title:
la $a0, title
syscall
li $v0, 4 #Print the Title stored at $s6 [Offset: 8]
la $a0, 8($s6)
syscall
li $v0, 4 #Print Description:
la $a0, description
syscall
li $v0, 4 #Print descript stored at $s6 [Offset: 72]
la $a0, 72($s6)
syscall
j afterprintpo
exit:
li $v0, 4 #Say
la $a0, goodbye #Goodbye!
syscall
li $v0, 10 #Terminate Program
syscall
I need help with debugging the following code. I've tried like 10 times or more but I still don't understand why I got such a weird output as seen below:
Enter a: 5
Enter b: 2
a/b = 268501012 (<--- weird output)
# task4partial.asm
# Given positive integers a and b, output a/b and a%b.
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz "a/b = "
str4: .asciiz "a%b = "
newline: .asciiz "\n"
.text
main: li $v0, 4 # system call code for print_string
la $a0, str1 # address of str1
syscall # print str1
#get the first number from user, put it into $s0
li $v0, 5 # system call code for read_int
syscall # read an integer into $v0 from console
add $s0, $v0, $zero # copy $v0 into $s0 (a)
#read print_string for str2
li $v0, 4 # system call code for print_string
la $a0, str2 # address of str1
syscall # print str1
# get second number from user, put it into $t1
li $v0, 5 #load syscall for read_int
syscall #make the syscall
move $s1, $v0 #move the number read into $s1(b)
#do the calculations
div $s0, $s1 #diving $s0 by $s1
mflo $t0 #storing value of lo(quotient) in
#register $t0
mfhi $t1 #storing value of hi(remainder) in
#register $t1
#read print_string for str3
li $v0, 4 # system call code for print_string
la $a0, str3 # address of str1
syscall # print str1
#print a/b
li $v0, 1 #load syscall print_int into $v0
move $t2, $t0 #move the number to print into $t2
syscall
#end of program
li $v0, 10 #system call code for exit
syscall
The mistake is in this line: move $t2, $t0 #move the number to print into $t2
The argument for the print_int syscall-routine has to be put into $a0, not into $t2.