how do I implement a jump address table in MIPS64? [duplicate] - mips

C++ Program
# include < iostream >
# include <string >
using namespace std;
int main ()
{
int resistance ; // in Ohms
string partNum ; // Part Number
cout << " Enter resistance : " << endl ;
cin >> resistance ;
switch ( resistance )
{
case 3 : partNum = " OD30GJE "; break ;
case 10 : partNum = " OD100JE "; break ;
case 22 : partNum = " OD220JE "; break ;
default : partNum = "No match "; break ;
}
cout << " Part number : " << partNum << endl ;
return 0;
}
Translate the C code to MIPS assembly code, add to your code the capability to match the closest resistor. Be sure to use the jr instruction for the switch statement. Have your code get the resistance as input from the user and display to the console the resistor's corresponding or closest part number.
Mips Assembly Code
.data
int_value: .space 20
.align 2
input: .asciiz "Enter resistance.\n" # declaration for string variable,
string1: .asciiz "OD30GJE\n" # declaration for string variable,
string2: .asciiz "OD100JE\n"
string3: .asciiz "OD220JE\n"
string11: .asciiz "No Match\n"
string12: .asciiz "Enter resistance\n"
.text
main:
li $v0, 4
la $a0, input # print for input
syscall
la $t0, int_value
li $v0, 5 # load appropriate system call code into register $v0;
syscall # call operating system to perform operation
sw $v0, int_value # value read from keyboard returned in register $v0;
# store this in desired location
lw $s1, 0($t0)
condition1:
slt $t1, $s1, $zero # if $s1 < 0 $t1 = 1 else $t1 = 0
beq $t1, $zero, condition2 # if $t1 = 0; InvalidEntry
bne $t1, $zero, invalid_entry
condition2:
sgt $t1, $s1, -1 # if $s1 > -1 then $t1 = 1 else $t1 = 0
beq $t1, $zero, invalid_entry # if $t1 = 0; InvalidEntry
sgt $t1, $s1, 9 # if s1 > 9 t1 = 1 else $t1 = 0
bne $t1, $zero, condition3 # if $t1 does not equal = 0; condition3
li $v0, 4
la $a0, string1
syscall
j exit
condition3:
sgt $t1, $s1, 9 # if $s1 > 9 then $t1 = 1 else $t1 = 0
beq $t1, $zero, invalid_entry # if $t1 = 0; InvalidEntry
sgt $t1, $s1, 21 # if s1 > 21 t1 = 1 else $t1 = 0
bne $t1, $zero, condition3 # if $t1 does not equal = 0; condition3
li $v0, 4
la $a0, string2
syscall
j exit
invalid_entry:
li $v0, 4
la $a0, string11
syscall
j exit
exit:
li $v0, 10 # v0<- (exit)
syscall

The following shows how to make a jump table. The offset within the jump table is assumed to be in $s1. $s1 must be a multiple of 4 (i.e. a byte offset).
This code has not been tested!
. . . .
b 1f # jump past the jump table
nop # branch delay slot
# table of jump addresses
JTAB: .word LABEL1
.word LABEL2
.word LABEL3
1: la $t0, JTAB # load start address of the jump table
add $t0, $t0, $s1 # add offset to table address
lw $t1, 0($t0) # load the address stored at JTAB + $s1
jr $t1 # and jump to that address
nop # branch delay slot
LABEL1: # do something
b 2f # break
nop
LABEL2: # do something else
b 2f # break
nop
LABEL3: # do a different thing
b 2f # break
nop
2: # after the end of the "case statement"

Related

Trying to print ints in MIPS, coming out as ASCII?

Here is some relevant code. I extracted these ints from a .txt file into buffer, and then use extractInts to format them into an array (intArray). Then I use printArray to loop through it and print. The ints in intArray are stored in a .space of 80 bytes, each in their own word.
My current output:
The array: 14385 57 14130 53 14388 13873 50 13109 13366 14393
My expected output:
The array: 18 9 27 5 48 16 2 53 64 98
# put ints into array
extractInts:
la $s0, buffer # $s0 points to buffer
la $s1, intArray # $s1 points to intArray
li $t0, 0 # i = 0
li $t1, 3 # intLoop max = 3
li $t3, 4 # nextWord max = 4
# loop to place each int within its own word boundary in intArray
intLoop:
beq $t0, $t1, exitLoop # exit loop if i = end of word
lb $t2, ($s0) # $t2 = intArray[]
beq $t2, '\n', nextWord # if $t2 = \n, go to nextWord
beq $t2, '\0', exitLoop # if $t2 = \0, go to exitLoop
sb $t2, ($s1) # save byte in $t2 to intArray[]
addi $t0, $t0, 1 # i++
addi $s0, $s0, 1 # increment buffer
addi $s1, $s1, 1 # increment intArray
j intLoop
# skip to next word
nextWord:
sub $t4, $t3, $t0 # $t4 = 4 - i
add $s1, $s1, $t4 # increment intArray by $t4
li $t0, 0 # restore i to 0
addi $s0, $s0, 1 # increment buffer
j intLoop
printArray:
li $t0, 0 # i = 0
while:
beq $t0, 80, exit # exit if i = end of array
lw $t1, intArray($t0) # $t1 = intArray[$t0]
addi $t0, $t0, 4 # move to next word in intArray
# print current int
li $v0, 1
move $a0, $t1
syscall
# print space
li $v0, 11
li $a0, 32
syscall
j while

Why is MIPS printing memory locations on the second time around

[Solved]
We were tasked with writing a bubble sort in MIPS and printing both the unsorted and sorted arrays
I've written the following code to accomplish this.
But when I go to run it, the original array prints as I would expect
Original Array
[34, 23, -1, -2, 0]
But then it prints this for the sorted array.
Sorted Array
[173496791, 1818324585, 1920090400, 686433, 1919898378]
(the Unicode squares do print)
array: .word 34, 23, -1, -2, 0, 89
length: .word 6
original: .asciiz "Original Array\n"
sorted: .asciiz "\nSorted Array\n"
.text
# main()
main:
la $a2, length
lw $a2, 0($a2) # a2 <= n_element
add $t9, $0, $0 # t9 : counter
la $a1, array # a1 = &array[0]
addi $t0, $0, 0 # for t0=0 to
addi $a2, $a2, -1 # because we do [i+1]
#says prints original array
la $a0, original
li $v0, 4
syscall
#prints array
jal print
#printing the string saying sorted
la $a0, sorted
li $v0, 4
syscall
#after printing original we reset the values?
la $a2, length
lw $a2, 0($a2) # a2 <= n_element
add $t9, $0, $0 # t9 : counter
add $t8, $0, $0 # t10 : counter 2
la $a1, array # a1 = &array[0]
addi $t0, $0, 0 # for t0=0 to
addi $a2, $a2, -1 # because we do [i+1]
#see if that worked it did
li $s0, 1 #boolean swapped = false 0=false 1=true
outerLoop:
beqz $s0, exit #if flag is false exit
li $s0, 0
addi $a1, $a1, 4 # next array offset
addi $t9, $t9, 1 # counter++
move $t8 $0
sub $s2, $a2, $t9
innerLoop:
bge $t8, $s2, exitInner
lw $t7, 0($a1) # t7 = array[i]
lw $t8, 4($a1) # t8 = array[i+1]
ble $t7, $t8, skip #if array[i]<= arr[i+1] skip the swapping
sw $t8, 0($a1) #a[i+1] = a[1]
sw $t7, 4($a1) #a[i] = a[i+1]
li $s0, 1#flag update
skip:
addi $t8, $t8, 1 #count
addi $a1, $a1, 4
j innerLoop
exitInner:
j outerLoop
exit:
la $a2, length
lw $a2, 0($a2) # a2 <= n_element
add $t9, $0, $0 # t9 : counter
addi $a2, $a2, -1 # because we do [i+1]
jal print
j quit#end Program
#print array
print:
lw $t7,0($a1) # t7 = array[i]
lw $t8,4($a1) # t8 = array[i+1]
add $a0, $t7, 0
addi $v0, $0, 1 #print int using system call the value stored in a0
syscall
addi $a0, $0, 0x20 # ascii 0x20 = ' '
addi $v0, $0, 11
syscall
addi $a1, $a1, 4 # next array offset
addi $t9, $t9, 1 # counter++
beq $t9, $a2, return #return to the where we were we were at beforehand
j print
return:
jr $ra
quit:
addi $v0, $0, 10
syscall
I think these are memory locations? I'm not sure why it's printing that as it is because I reset my counter values before trying to print. So the question is. Why am I printing memory locations? if that is what is happening
I was sorting in place so I had to reset the array before the print function.

Binary Search using recursion in Mips assembly

I have a trouble. I tried to make a binary search algorithm using recursion in mips assembly, but I have some errors that I don't understand how to solve them.
I have an array of 10 integers and I assume that the array is sorted.
this is my code, I appreciate any help and thanks in advance..
.data
arr: .word 40
arrMsg: .asciiz "Enter the array : \n"
posMsg: .asciiz "This value exist in the array and its position is "
pos: .word 0
newline: .asciiz "\n"
valMsg: .asciiz "Enter the value you search for : \n"
val: .word 0
notfound:.asciiz "the value doesn't exist in the array !! \n"
.text
main:
# print the array message
li $v0, 4
la $a0, arrMsg
syscall
# read the array from the user
# put $s0 = i
add $s0, $zero, $zero # i = 0
for:
beq $s0, 40, end
li $v0, 5
syscall
sw $v0, arr($s0) # input arr[i]
addi $s0, $s0, 4 # i = i + 4
j for
end:
# print value message
li $v0, 4
la $a0, valMsg
syscall
# read the value from the user
li $v0, 5
syscall
# store the value in the val variable
sw $v0, val
################################################
## put $s0 = start , $s1 = middle , $s2 = end ##
################################################
li $s0, 0
li $s2, 9
jal BinarySearch
li $v0, 10
syscall
############################################################################################################
BinarySearch:
# middle = (start + end ) / 2
add $t0, $s0, $s2 # $t0 = start + end
sra $s1, $t0, 1 # $s1 = $t0 / 2
# save $ra in the stack
addi $sp, $sp, -4
sw $ra, 0($sp)
# base case
ble $s2, $s0, returnNegative1 # if (end <= start)
lw $t1, arr($s1) # $t1 = arr[middle]
lw $t2, val # $t2 = val
beq $t1, $t2, returnMiddle # if (arr[middle] == val)
blt $t2, $t1, returnFirstPart # if (val < arr[middle])
bgt $t2, $t1, returnLastPart # if (val > arr[middle])
returnNegative1:
li $v0, -1
j Exit
returnMiddle:
move $v0, $s1 # return middle
j Exit
returnFirstPart:
move $t3, $s1 # temp = middle
addi $t3, $t3, -1 # temp --
move $s2, $t3 # end = temp
jal BinarySearch
j Exit
returnLastPart:
move $t3, $s1 # temp = middle
addi $t3, $t3, 1 # temp++
move $s0, $t3 # start = temp
jal BinarySearch
j Exit
Exit:
lw $ra, 0($sp)
addi $sp, $sp, 4`
jr $ra
lw $t1, arr($s1) # $t1 = arr[middle]
this is the problem as it is not really the right index as integer takes 4 bytes
so the middle you get from
add $t0, $s0, $s2 # $t0 = start + end
sra $s1, $t0, 1 # $s1 = $t0 / 2
is just the logical address not the real one you will need to multiply it with 4
mul $s4, $s1,4
and then use $s4 as an address
lw $t1, arr($s4) # $t1 = arr[middle]
also there is a mistake with the stopping condition it should be
if (end < start) not (<=)
and sorry for my English

MIPS: Lucas Sequence Returns P every other number when P is 1

I am implementing a mips program for a lucas sequence. The user enters a number, n, P, and Q and choose 1 for the V sequence and 0 for the U sequence. I am having issues with choosing the V sequence returning P every other number only when P is 1. I know it is something little that I am missing, but I have been trying to find it for hours. Any help would be greatly appreciated.
addi $t1, $s0, 0 # load n into $t1
addi $t3, $zero, 0 # i = 0
jal lucasSequence # go to lucasSequence
end_loop:
la $a0, newline # print a newline \n
jal printString
j main # loop to main menu again
#############################################
# Procedure: lucasSequence #
#############################################
# - produces the Lucas sequence of the #
# first (U) or second (V) order for #
# given constants P and Q. #
# #
# The procedure produces all numbers #
# in the sequence U or V from n=0 #
# up to n=N. #
# #
# - inputs : $a0-integer N #
# $a1-constant P #
# $a2-constant Q #
# $a3-function U (0) or V (1) #
# - outputs: none #
# #
#############################################
lucasSequence:
loop:
move $a0, $t3 # n = i
beq $t3, $t1, end_loop # if i == n, quit.
jal lucasSequenceNumber # print the lucas sequence for N, P, and Q
addi $t3, $t3, 1 # i++
move $a0, $v0 # load int to print
li $v0, 1 # print int
syscall # print call
beq $t3, $t1, end_loop # don't print last comma
li $a0, ',' # load comma
li $v0, 11 # add to print
syscall # print comma
li $a0, ' ' # load space
li $v0, 11 # print space
syscall # print call
j loop # repeat until i = n
lucasSequenceNumber:
addi $sp, $sp, -8 # room for $ra and one temporary
sw $ra, 4($sp) # save $ra
move $v0, $a0 # pre-load return value as n
addi $t4, $zero, 1 # t4 =1
blt $a0, 2, rt # if(n < 2) return
sw $a0, 0($sp) # save a copy of n
addi $a0, $a0, -1 # n - 1
jal lucasSequenceNumber # lucas(n - 1)
lw $a0, 0($sp) # retrieve n
mul $v0, $v0, $a1 # P*lucas(n-1)
sw $v0, 0($sp) # save result of P*lucas(n - 1)
addi $a0, $a0, -2 # n - 2
jal lucasSequenceNumber # lucas(n - 2)
mul $v0, $v0, $a2 # Q*lucas(n-2)
lw $v1, 0($sp) # retrieve P*lucas(n - 1)
sub $v0, $v1, $v0 # P*lucas(n - 1) + Q*lucas(n - 2)
rt:
addi $t4, $zero, 1 # t4 = 1
beq $a3, $t4, return_V # user chose option 2
lucas_rt:
lw $ra, 4($sp) # restore $ra
addi $sp, $sp, 8 # restore $sp
jr $ra # back to caller
return_V:
bgt $a0, $t4, lucas_rt # if choice V and n > 1, got to return
beq $a0, $t4, one_v # if n = 1, go to one_v
li $a0, 2 # if n = 0, return 2
move $v0, $a0
j lucas_rt # back to caller
one_v:
addi $a0, $a1, 0 # return P
move $v0, $a0
j lucas_rt # go to return P call
The function one_v needs to be adjusted so it reads:
one_v:
move $a0, $a1
move $a0, $v0
j lucas_rt

Mips loop iteration

I have the following code which is effectively supposed to increment from 1-6 while doing arithmetic on a variable.
An example in C would be
int res = 0;
for(i=1; i<6; i++) {
res += i*i+1;
}
Now for my mips code:
.data
res: .word 0
.text
li $t0, 1 #int j = 1
loop:
beq $t0, 6, end #for(int j = 1; j < 6; j++)
add $t0, $t0, 1
mul $t1, $t0, $t0 #res += j * j + 1
addi $t1, $t1, 1
sw $t1, res
b loop
end:
li $v0, 1 #checking the end result
la $a0, res
syscall
li $v0, 10 #graceful exit
syscall
For some reason the result I get towards the end is ~300 million and that is obviously wrong. Does anyone see what the issue is? Im fairly new to mips assembly.
Thanks
You've got a few problems here.
Firstly, the line you have marked #res += j * j + 1 is only multiplying, there is no addition involved.
You seem to be attempting to use res to store a running total but you are overwriting it in each iteration. In fact, there shouldn't be any need to store res in memory, a register is much more appropriate for this purpose.
.text
main:
move $t2 $zero
li $t0 1
loop:
mul $t1 $t0 $t0
addi $t1 $t1 1
add $t2 $t2 $t1 # res += i * i + 1
addi $t0 $t0 1
blt $t0 6 loop
# print res
li $v0 1
move $a0 $t2
syscall
# exit
li $v0 10
syscall
Prints 60.
You want to do something like this.
I am assuming you wanted something like this:
int res = 0;
for(i=1; i<6; i++) {
res += i*i+1;
}
.data
res: .word 0
.text
li $t0, 1 #int i = 1
loop:
bgt $t0, 6, exit
add $t0, $t0, 1
mul $t1, $t0, $t0
addi $t1, $t1, 1
sw $t1, res // I'm sure you can just move here
b loop
exit: