Our team is trying to create a compiler that's fed code and produces MIPS assembly from it.
To tackle array declaration at the global scope, we create a label for the array in .text and reserve 4 bytes to hold the address pointing to the start of the array in memory.
.text
arr: .space 4
.data
...
li $t0, N # (where N = number of elements in arr)
li $v0, 9 # Load system instruction to allocate dynamic memory
li $t1, 4 # 4 bytes per element
mult $t0, $t1 # Calculates how big the allocated memory has to be in bytes.
mflo $a0 # Loads this value into $a0
syscall # Allocates memory and returns address into $v0
la $s0, arr # load address of arr into $s0
sw $v0, ($s0) # Save allocated memory address into the space reserved in .text
However, the last instruction doesn't seem to be working properly for us.
This image shows exactly where the error occurs and the state of the registers at that time. I'm unsure why it's causing an error.
Edit: some more information on the error produced, updated to encompass the modified instructions at the end
[00400000] 8fa40000 lw $4, 0($29) ; 183: lw $a0 0($sp) # argc
[00400004] 27a50004 addiu $5, $29, 4 ; 184: addiu $a1 $sp 4 # argv
[00400008] 24a60004 addiu $6, $5, 4 ; 185: addiu $a2 $a1 4 # envp
[0040000c] 00041080 sll $2, $4, 2 ; 186: sll $v0 $a0 2
[00400010] 00c23021 addu $6, $6, $2 ; 187: addu $a2 $a2 $v0
[00400014] 0c100009 jal 0x00400024 [main] ; 188: jal main
[00400018] 00000000 nop ; 189: nop
[0040001c] 3402000a ori $2, $0, 10 ; 191: li $v0 10
[00400020] 0000000c syscall ; 192: syscall # syscall 10 (exit)
[00400024] 34080003 ori $8, $0, 3 ; 9: li $t0, 3 # Load immediate value into register $t0
[00400028] 34020009 ori $2, $0, 9 ; 10: li $v0, 9
[0040002c] 34090004 ori $9, $0, 4 ; 11: li $t1, 4
[00400030] 01090018 mult $8, $9 ; 12: mult $t0, $t1
[00400034] 00002012 mflo $4 ; 13: mflo $a0
[00400038] 0000000c syscall ; 14: syscall
[0040003c] 3c101001 lui $16, 4097 [arr0] ; 15: la $s0, arr0
[00400040] ae020000 sw $2, 0($16) ; 16: sw $v0, ($s0)
PC = 400040
EPC = 40003c
Cause = 1c
BadVAddr = 1004002f
Status = 3000ff12
The last instruction sw $v0, arr0($0) = MEM[$0 + arr0] = $v0 which isn't correct. However you'll be given the amount of memory requested and it is needed to save that memory pointer in a register until scope of program. The assignment might look like this,
arr: .space 4
syscall # Allocates memory and returns address into $v0
la $s0, arr # arr is pointer address
st $v0, 0($s0) # start base address of array
Now arr is base address of 4 bytes of memory holding the address of memory content. For good practice it is important to de-allocate the memory assigned back to OS as,
li $v0,10 # return back to OS
syscall
Related
.org 0x10000000
# Initializations
# NOTE: You may add initializations after line 10, but please do not
# remove or change the initializations to $sp, $s0, $s1, or $s2
li $sp, 0x10fffffc # Starting address of empty stack
li $s0, 0xf0000000 # UART base address
li $s1, array_ptr # Array head pointer
li $s2, array_ptr # Array tail pointer
li $t8 0x00000000
####################################################################
# Do not make changes to the jump to main, the allocation of
# memory for the array, or the main loop
####################################################################
j main
nop
array_ptr: # Label pointing to 100 word array
.space 100
main:
jal poll_UART
nop
jal period_check
nop
jal space_check
nop
jal case_check
nop
jal array_push
nop
j main
nop
####################################################################
# ******************************************************************
####################################################################
# The "poll_UART" function should poll the status register of the UART.
# If the 2^1 bit position (ready bit) is set to 1 then it
# should copy the receive buffer's value into $v0 and send
# a clear status command (2^1) to the command register before
# returning (a return statement is already included). In order to
# receive full credit, $s0 must contain the base address of the UART
# and must be used with the appropriate offsets to access UART
# registers and buffers
poll_UART:
lw $t1, 4($s0)
li $t2, 0b10
and $t3, $t1, $t2
beq $t3, $0, main
nop
lw $v0, 8($s0)
sw $t2, 0($s0)
jr $ra
nop
# The "period_check" function should check if the current character ($v0)
# is a period ("."). If it is a period then the function should go to the
# label, "palindrome_check". If the character is not a period then it
# should use the included return.
period_check:
li $t0, 0x2E
beq $v0, $t0, palindrome_check
nop
jr $ra
nop
# The "space_check" function should check if the current character ($v0)
# is a space (" "). If it is then it should jump to "main" so
# that it skips saving the space character. If not it should
# use the included return.
space_check:
li $t4, 0x20
beq $t4, $v0, main
nop
jr $ra
nop
# The "case_check" function should perform a single inequality check.
# If the current character ($v0) is greater than the ASCII value of 'Z',
# which indicates the current character is lowercase, then it should convert
# the value of $v0 to the uppercase equivalent and then return. If the
# current character ($v0) is already uppercase (meaning the inequality
# mentioned before was not true) then the function should return without
# performing a conversion.
case_check:
li $t5, 0x5A
slt $t6, $v0, $t5
li $t7, 1
beq $t6, $t7, change_uppercase
change_uppercase:
addiu $v0, $v0, -32
jr $ra
nop
# The "array_push" function should save the current character ($v0) to the
# current location of the tail pointer, $s2. Then it should increment the
# tail pointer so that it points to the next element of the array. Last
# it should use the included return statement.
array_push:
sw $v0, 0($s2)
addiu $s2, $s2, 4
jr $ra
nop
# The "palindrome_check" subroutine should be jumped to by the period
# check function if a period is encountered. This subroutine should contain
# a loop that traverses the array from the front towards the back (using the
# head pointer, $s1) and from the back towards the front(using the tail
# pointer, $s2). If the string is a palindrome then as the array is traversed
# the characters pointed to should be equal. If the characters are not equal
# then the string is not a palindrome and the print function should be used
# to print "No". If the pointers cross (i.e. the head pointer's address is
# greater than or equal to the tail pointer's address) and the compared
# characters are equal then the string is a palindrome and "Yes" should be
# printed.
#
# Remember to restore the head and tail pointers to the first element
# of the array before the subroutine jumps back to main to begin processing the
# next string. Also, keep in mind that because the tail pointer is updated at
# the end of "array_push" it technically points one element past the last
# character in the array. You will need to compensate for this by either
# decrementing the pointer once at the start of the array or using an offset
# from this pointer's address.
palindrome_check:
addiu $s2, $s2, -8
move $s3, $s1
subu $s4, $s2, $s3
beq $s4, $0, palindrome
nop
check_loop:
lw $s5, 0($s3)
lw $s6, 0($s2)
bne $s6, $t5, not_palindrome
nop
adjust_pointers:
addiu $s2, $s2, -4
addiu $s3, $s3, 4
slt $t8, $s3, $s2
bne $t8, $0, check_loop
nop
j palindrome
nop
palindrome:
li $a0, 1
call project3_print
move $s2, $s1
j main
nop
not_palindrome:
li $a0, 0
call project3_print
move $s2, $s1
j main
nop
I have tried working on debugging the Palindrome check and everything under that but it all appears fine. Im not sure if the problem remains in the poll_uart or not. this is the template I was given.
When I type in racecar it says no. when I type in gn. it says yes. when I type in night. it says no. So I am just confused.
I am working on a project which checks if a word is a palindrome and sends back "yes" or "no". For example the input "abba. data. abcd. reviver." should read "yes" "no" "no" "yes". Instead what I get is all "no". I have done some debugging and from what I can tell I am missing something in the palindrome_loop that is saying everything is not a palindrome. I am very new to all this so I am sorry if I sound uneducated in all this and kind of lost, but I am unsure of what else to do next any guidance or feedback is appreciated.
I am using PLPtool
Below is my code:
.org 0x10000000
# Initializations
# NOTE: You may add initializations after line 10, but please do not
# remove or change the initializations to $sp, $s0, $s1, or $s2
li $sp, 0x10fffffc # Starting address of empty stack
li $s0, 0xf0000000 # UART base address
li $s1, array_ptr # Array head pointer
li $s2, array_ptr # Array tail pointer
####################################################################
# Do not make changes to the jump to main, the allocation of
# memory for the array, or the main loop
####################################################################
j main
nop
array_ptr: # Label pointing to 100 word array
.space 100
main:
jal poll_UART
nop
jal period_check
nop
jal space_check
nop
jal case_check
nop
jal array_push
nop
j main
nop
####################################################################
# ******************************************************************
####################################################################
# The "poll_UART" function should poll the status register of the UART.
# If the 2^1 bit position (ready bit) is set to 1 then it
# should copy the receive buffer's value into $v0 and send
# a clear status command (2^1) to the command register before
# returning (a return statement is already included). In order to
# receive full credit, $s0 must contain the base address of the UART
# and must be used with the appropriate offsets to access UART
# registers and buffers
poll_UART:
lw $t1, 4($s0)
li $t2, 0b10
and $t3, $t2, $t1
beq $t3, $zero, main
nop
lw $v0, 8($s0)
sw $t2, 0($s0)
jr $ra
nop
# The "period_check" function should check if the current character ($v0)
# is a period ("."). If it is a period then the function should go to the
# label, "palindrome_check". If the character is not a period then it
# should use the included return.
period_check:
li $t0, 0x2E
beq $v0, $t0, palindrome_check
nop
jr $ra
nop
# The "space_check" function should check if the current character ($v0)
# is a space (" "). If it is then it should jump to "main" so
# that it skips saving the space character. If not it should
# use the included return.
space_check:
li $t4, 0x20
beq $t4, $v0, main
jr $ra
nop
# The "case_check" function should perform a single inequality check.
# If the current character ($v0) is greater than the ASCII value of 'Z',
# which indicates the current character is lowercase, then it should convert
# the value of $v0 to the uppercase equivalent and then return. If the
# current character ($v0) is already uppercase (meaning the inequality
# mentioned before was not true) then the function should return without
# performing a conversion.
case_check:
li $t5, 0x5A
slt $t6, $v0, $t5
li $t7, 1
beq $t6, $t7, convert_lowercase
nop
convert_lowercase:
addiu $v0, $v0, -32
jr $ra
nop
# The "array_push" function should save the current character ($v0) to the
# current location of the tail pointer, $s2. Then it should increment the
# tail pointer so that it points to the next element of the array. Last
# it should use the included return statement.
array_push:
sw $v0, 0($s2)
addiu $s2, $s2, 4
jr $ra
nop
# The "palindrome_check" subroutine should be jumped to by the period
# check function if a period is encountered. This subroutine should contain
# a loop that traverses the array from the front towards the back (using the
# head pointer, $s1) and from the back towards the front(using the tail
# pointer, $s2). If the string is a palindrome then as the array is traversed
# the characters pointed to should be equal. If the characters are not equal
# then the string is not a palindrome and the print function should be used
# to print "No". If the pointers cross (i.e. the head pointer's address is
# greater than or equal to the tail pointer's address) and the compared
# characters are equal then the string is a palindrome and "Yes" should be
# printed.
#
# Remember to restore the head and tail pointers to the first element
# of the array before the subroutine jumps back to main to begin processing the
# next string. Also, keep in mind that because the tail pointer is updated at
# the end of "array_push" it technically points one element past the last
# character in the array. You will need to compensate for this by either
# decrementing the pointer once at the start of the array or using an offset
# from this pointer's address.
palindrome_check:
addiu $s2, $s2, -8
move $s3, $s1
subu $s6, $s2, $s3
beq $s6, $zero, palindrome
nop
palindrome_loop:
lw $s4, 0($s3)
lw $s5, 0($s2)
bne $s5, $t0, not_a_palindrome
nop
pointer_adjust:
addiu $s2, $s2, -4
addiu $s3, $s3, 4
slt $t8, $s3, $s2
bne $t8, $t0, palindrome_loop
nop
j palindrome
nop
palindrome:
li $a0, 1
call project3_print
move $s2, $s1
j main
nop
not_a_palindrome:
li $a0, 0
call project3_print
move $s2, $s1
j main
nop
This is the C source code
#include <stdio.h>
int main() {
printf("The Factorial of 10 is %d\n", fact(10));
}
int fact(int n) {
if (n < 1)
return (1);
else
return (n * fact(n - 1));
}
I am converting a C Programming function to a MIPS, but when I run the MIPS program I am getting an error for the .ascii section.
.text
.globl main
main:
subu $sp,$sp,32 # Stack frame is 32 bytes long
sw $ra,20($sp) # Save return address
sw $fp,16($sp) # Save old frame pointer
addiu $fp,$sp,28 # Set up frame pointer
li $a0,10 # Put argument (10) in $a0
jal fact # Call factorial function
la $a0,$LC # Put format string in $a0
move $a1,$v0 # Move fact result to $a1
jal printf # Call the print function
lw $ra,20($sp) # Restore return address
lw $fp,16($sp) # Restore frame pointer
addiu $sp,$sp,32 # Pop stack frame
jr $ra # Return to caller
.rdata
$LC:
.ascii “The factorial of 10 is %d\n\000”
.text
fact:
subu $sp,$sp,32 # Stack frame is 32 bytes long
sw $ra,20($sp) # Save return address
sw $fp,16($sp) # Save frame pointer
addiu $fp,$sp,28 # Set up frame pointer
sw $a0,0($fp) # Save argument (n) to use for Recursive Call
lw $v0,0($fp) # Load n
bgtz $v0,$L2 # Branch if n > 0
li $v0,1 # Return 1
jr $L1 # Jump to code to return
$L2:
lw $v1,0($fp) # Load n
subu $v0,$v1,1 # Compute n - 1
move $a0,$v0 # Move value to $a0
jal fact # Call factorial function
lw $v1,0($fp) # Load n
mul $v0,$v0,$v1 # Compute fact(n-1) * n
$L1: # Result is in $v0
lw $ra, 20($sp) # Restore $ra
lw $fp, 16($sp) # Restore $fp
addiu $sp, $sp, 32 # Pop stack
jr $ra # Return to caller
It's giving me an error for the .ascii code section saying it shouldn't be in the .text:
Error in ".ascii" directive cannot appear in text segment
It's also saying that:
"$L1": operand is of incorrect type
It's giving me an error for the .ascii code section saying it shouldn't be in the .text:
Error in ".ascii" directive cannot appear in text segment"
I am going out on a limb here because I am not 100% sure what you are running this on, but some sims like MARS don't recognize the rdata segment. You can try using just .data.
Also, if you are on something like WinMIPS64, you may want to try placing the .data segment at the top of the code. I understand what you are doing is right in some environments and but doesn't work in others, so give it a whirl.
May I suggest you try these things separately, just in case.
I was trying to do an assignment translating from C to MIPS but I get a instruction reference error in jal main. Here is what I have to translate:
void swap (int a, int b)
{
int temp=a;
a=b;
b=temp;
}
int distance (int a, int b)
{
if (b > a)
swap (a,b);
return (a-b)
}
And here is what I wrote:
.data #declare the variables
var1: .word 4, 7, 12, 5
var2: .word 15, 3, 6, 14
result: .space 4
.text
main:
la $t0, var1 #load address 'var1' into $t0
la $t1, var2 #load address 'var2' into $t1
la $t2, result #load address 'result' into $t2
li $t3, 0 # load imm (i=0)
for_loop:
bgt $t3, 4, for_done #when i>4 do not meet condition, exit
lw $t4, 0($t4) #result[i] = tmp
jal distance
addi $t3, $t3, 1 #i++
j for_loop
for_done:
la $t2, distance
ori $v0, $0, 4
syscall
distance:
blt $t0, $t1, exit
jal swap
sub $t5, $t0, $t1
jr $t5
swap:
lw $t6, 0($t0)
lw $t7, 0($t1)
sw $t6, 0($t1)
sw $t7, 0($t0)
exit:
I actually don't know what am I doing, just the basics of Assembly. I hope some of you could help me. :)
I'm sorry but your asm code had [at least] 15 bugs.
I've created two versions: one with the bugs annotated and a second with the bugs fixed and a working program
Note that due to the vagueness of the C code, I had to guess the true intent of the program and take [considerable] poetic license.
Here's the unchanged version with the bugs annotated [please pardon the gratuitous style cleanup]:
.data # declare the variables
var1: .word 4, 7, 12, 5
var2: .word 15, 3, 6, 14
# NOTE/BUG: this only reserves 4 bytes instead of the 16 we need to hold all
# four values
result: .space 4
.text
main:
la $t0,var1 # load address 'var1' into $t0
la $t1,var2 # load address 'var2' into $t1
la $t2,result # load address 'result' into $t2
li $t3,0 # load imm (i=0)
for_loop:
# NOTE/BUG: this goes one too far (i.e. we want i>=4)
bgt $t3,4,for_done # when i>4 do not meet condition, exit
# NOTE/BUG: $t4 is never intialized to anything, so this instruction will fault
# (e.g. equivalent to dererencing a null pointer in C)
lw $t4,0($t4) # result[i] = tmp
jal distance
# NOTE/BUG: the index variable 'i' is incremented, but distance does _not_ use
# (i.e.) distance will always use var1[0] and var2[0] on each iteration
addi $t3,$t3,1 # i++
j for_loop
# NOTE/BUG: what do we want to do here? -- print the result vector presumably
# NOTE/BUG: syscall 4 is to print a string -- it would require setting up $a0
# and _not_ $t2 but, even then, using 'distance' is wrong as distance is the
# function name and _not_ a string so we'd get garbage
# NOTE/BUG: we probably wouldn't even get that far because QtSpim would
# probably fault because distance is in the .text segment and not the .data
# segment
for_done:
la $t2,distance
ori $v0,$0,4
syscall
distance:
# NOTE/BUG: this is comparing _addresses_ instead of _values_ (i.e.) this
# compares (&var1[i] > &var2[i]) instead of var1[i] > var2[i])
# NOTE/BUG: this test is _reversed_, because this guarantees negative numbers
blt $t0,$t1,exit
# NOTE/BUG: jal is calling swap as a function, but swap is merely a label here
jal swap
# NOTE/BUG: based on the mips ABI, return values go into $v0
sub $t5,$t0,$t1
# NOTE/BUG: when 'jal distance' is called, the return address goes into $ra
# and to return to the place in main that called us, we want to do 'jr $ra'
# NOTE/BUG: this 'jr' should be at exit:
jr $t5
# NOTE/BUG: this actually swaps var1[i] and var2[i] -- would this be correct to
# modify the original arrays???
swap:
lw $t6,0($t0)
lw $t7,0($t1)
sw $t6,0($t1)
sw $t7,0($t0)
# NOTE/BUG: this is where the 'jr' should go
exit:
Here's the cleaned up and working version. I decided that it should store the distance in the result vector and then show all three vectors:
.data
# NOTE: lw/sw must be four byte aligned so keep these first
var1: .word 4, 7, 12, 5
var2: .word 15, 3, 6, 14
result: .space 16
msg_var1: .asciiz "var1:"
msg_var2: .asciiz "var2:"
msg_result: .asciiz "dist:"
msg_space: .asciiz " "
msg_nl: .asciiz "\n"
.text
main:
la $s0,var1 # load address of 'var1'
la $s1,var2 # load address of 'var2'
la $s2,result # load address of 'result'
li $s3,4 # number of elements in a given vector
li $s4,0 # load imm (i=0)
for_loop:
bge $s4,$s3,for_done # i <= count? if no, fly
jal distance
addi $s4,$s4,1 # i++
j for_loop
for_done:
la $a0,msg_var1
la $a1,var1
jal show
la $a0,msg_var2
la $a1,var2
jal show
la $a0,msg_result
la $a1,result
jal show
# exit program
li $v0,10
syscall
# distance -- calculate distance between two numbers in two vectors
#
# RETURNS:
# stores into 'result' vector
#
# global registers:
# s0 -- pointer to var1
# s1 -- pointer to var2
# s2 -- pointer to result
# s4 -- array index
#
# registers:
# t0 -- address and value of var1[i]
# t1 -- address and value of var2[i]
# t2 -- temp value
# t7 -- byte offset corresponding to index 'i'
distance:
sll $t7,$s4,2 # convert index to byte offset
addu $t0,$s0,$t7 # get &var1[i]
lw $t0,0($t0) # fetch var1[i]
addu $t1,$s1,$t7 # get &var2[i]
lw $t1,0($t1) # fetch var2[i]
bge $t0,$t1,distance_done # swap a/b to get abs val? if no, fly
# swap a/b
move $t2,$t0 # temp = a
move $t0,$t1 # a = b
move $t1,$t2 # b = temp
distance_done:
sub $v0,$t0,$t1 # get distance (i.e.) abs(a-b)
addu $t2,$s2,$t7 # get &result[i]
sw $v0,0($t2) # result[i] = distance
jr $ra # return
# show -- show vector
#
# arguments:
# a0 -- vector name
# a1 -- pointer to vector
#
# registers:
# t3 -- array remaining count
#
# clobbers:
# v0
show:
li $v0,4 # syscall to print string
syscall
move $t3,$s3 # get number of elements in vector
show_loop:
blez $t3,show_done # more to do? if no, fly
li $v0,4
la $a0,msg_space # output a space
syscall
# output vector[i]
li $v0,1 # syscall to output value
lw $a0,0($a1) # get vector value
syscall
addiu $a1,$a1,4 # advance pointer to next array element
addi $t3,$t3,-1 # bump down count
j show_loop
show_done:
# output newline
la $v0,4
la $a0,msg_nl
syscall
jr $ra # return
I am trying to make a loop that will add user inputted integers into an array until it fills the array. Every time I typed in a value, QTSPIM spits out 268501016 which I assume to be some random value stored in an register.
To test if my program was going through the whole loop, I added a call to an ascii line when the program reached the branch portion of my beq. The program seemed to be branching even if the values were not (at least to my understanding) equal.
.data
array1: .space 24
str1: .ascii "Type in numbers:"
str2: .ascii "Reached Terminate"
.text
main:
li $t2, 5
li $t3, 0
loop1:
beq $t3, $t2, terminate #branch if equal
la $a0, str1
syscall
ori $v0, $0, 5 #instruction to store user input in v0
syscall #get user input and store it in v0
la $t4, array1 #load the address of the array
addu $t0, $0, $v0 #add v0 (our user input) to $t0
sw 0($t4), t0 #stores the value in $t4 to our array
addi $t3, $t3, 1 #add 1 to t3 (incrementing the counter)
addi $t4, $t4, 4 $add 4 to increment the array 4 bits to the next array slot
jal loop1
terminate:
la $a2, str2 #load the string to check when the program reaches terminate
syscall
ori $v0, $0, 10 # end the program
syscall
The only thing I can think is that my jump call is not going back to loop1, but if this is the case I am unaware how to fix that.
This is 32 bit MIPS code.
You're not setting up the registers properly before the syscalls.
Here there should be an li $v0, 4 before syscall:
la $a0, str1
syscall
If we assume that you're trying to print str2 here, there should be an li $v0, 4 before the syscall, and $a0 should be used instead of $a2:
la $a2, str2 #load the string to check when the program reaches terminate
syscall
This should be sw $t0, 0($t4), not the other way around:
sw 0($t4), t0
This should be j, not jal (jal is used for function calls):
jal loop1