MIPS - Accessing Instruction address to obtain opcode - mips

I'm working on a program where I loop through the code and count the number of I, J, and R type instructions. How would I go about accessing the opcode of the instructions?

.data
typeR: .word 0x00 #for all TypeR opc code
typeJ: .word 0x01 #we will have to left shit one bit when comparing
typeI: .word 0x11 #this is a dummy value because if it's not R or J, it is I
numR: .word 0
numJ: .word 0
numI: .word 0
endProgram: .word 0x11111111 # you would have to know the address of the #last instruction. Assume for now
.text
main:
lw $s0, countR
lw $s1, countJ
lw $s2, countI
#here you load the address of the first instruction of the program you try
#to loop though say 0x00000000
la $t0, 0x00000000
#here load the instruction code in 0x11111111 to register $t1
lw $t1, endProgram
Loop:
lw $s3, 0($t0) # load first instruction code to $s3
beq $s3, typeR, R #if equal goes to R
sll $s3, $s3, 1 # so we get rid of the first digit
beq $s3, typeJ, J # if equal got to J
J I # this is the else
R:
addi $s0,$s0, 1 #increment countR
j next
J:
addi $s1, $s1, 1#increment countJ
j next
I:
addi $s2, $s2, 1 # increment countI
j next
next:#check if it is the end yet, if not keep going to next instruction
lw $s5, 0($t1)
beq $s5, $t1, exit # if it is last instructin go to exit
#if not keep going
addi $t1, $t1, 4 #move to next instruction since each is 4 byte
j loop
exit:
# I think the last part is pretty simple, depend on what you want to do just #print it out or something, so I've save some typing.

Related

Printing an hourglass in mips32

I'm having problems solving this question. I want to print an hourglass using mips32. n is an integer given by user as input and the hourglass must be printed in n lines. For example, for n = 5 the output is:
*****
***
*
***
*****
Here is my code for the first part (the triangle top of the hourglass). The problem is that it prints only the first line of stars and then exits. By running my code line by line in Mars, I understood that the first line of the backToLoop1 label is run every time loop3 is run! So it causes the program to end after the first line. I really can't realize why this happens.
.data
newLine: .asciiz "\n"
.text
main:
li $v0, 5 # read n
syscall # call sysetem
addi $t2, $v0, 0 # moves n to $t2
li $t0, 1 # i= 1
loop1:
blt $t2, $t0, Exit # if n<i exit
la $a0, newLine # go to next line
addi $v0, $0, 4 # 4 represents printing string
syscall # call system
# loop2 bounds
li $t1, 1 # k= 1
subi $t3, $t0, 1 # $t3= i-1 upper bound for loop2
# loop3 bounds
li $t5, 1 # j= 1
addi $t6, $t2, 1 # t6= n+1
sub $t6, $t6, $t0 # $t6= n+1-i upper bound for loop3
loop2:
blt $t3, $t1, loop3
li $a0, ' ' # load space to $a0
la $v0, 11 # 11 represents printing character
syscall # call system
addi $t1, $t1, 1 # k++
ble $t1, $t3, loop2 # if <= i-1 loop2 again
loop3:
blt $t6, $t5, backToLoop1 # back to loop1
li $a0, '*' # load star to $a0
la $v0, 11 # 4 represents printing character
syscall # call system
addi $t5, $t5, 1 # j++
ble $t5, $t6, loop3 # if j <= n-i+1 loop3 again
backToLoop1:
addi $t0, $t0, 1 # i++
ble $t0, $t2, loop1 # if i<=n loop1 again
blt $t2, $t0, Exit
Exit: # Terminate the program
li $v0, 10 # 10 represents exit
syscall # call system
You're off to a good start. However, there doesn't appear to be a clear strategy for slanting the right side of the hourglass. Ideally we can write logic to handle drawing the bottom half without duplicating most of the logic.
My default approach for this sort of pattern is to use two pointers, a left starting at 0 and right starting at n - 1. These represent the index bounds for the asterisk characters for each row. Per row iteration, decrement the right pointer and increment the left pointer, essentially drawing an "X" pattern on the n by n grid.
This strategy gets us 95% of the way there. The last step is to temporarily swap the left and right pointers if left > right, which handles drawing the bottom half without too much spaghetti.
.data
prompt: .asciiz "enter a number: "
.text
main:
la $a0 prompt # collect n
li $v0 4
syscall
li $v0 5
syscall
move $s3 $v0 # n
li $s0 0 # left index
move $s1 $s3 # right index = n - 1
addi $s1 $s1 -1
row_loop:
bltz $s1 exit # while right-- >= 0
li $s2 0 # column index
col_loop:
beq $s2 $s3 row_loop_done # for 0..n
# if left > right, swap temporarily
move $t0 $s0
move $t1 $s1
blt $t0 $t1 pick_char
move $t2 $t0
move $t0 $t1
move $t1 $t2
pick_char:
# '*' if left <= i <= right else ' '
blt $s2 $t0 pick_space
bgt $s2 $t1 pick_space
li $a0 42 # print '*'
j print_char
pick_space:
li $a0 32 # print ' '
print_char:
li $v0 11
syscall
addi $s2 $s2 1 # column index++
j col_loop
row_loop_done:
li $a0 10 # print newline
li $v0 11
syscall
addi $s1 $s1 -1 # right--
addi $s0 $s0 1 # left++
j row_loop
exit:
li $v0 10
syscall

remu instruction expanded to include break instruction - ALWAYS stops program

I'm learning MIPS by using QtSpim. In the code below, a remu pseudo-instruction is expanded to include a break instruction. I imagine that the break is to catch a divide by zero, but in this case, it stops the code from functioning regardless:
.text
.globl main
main: li $s0, 113 # $s0 holds n, the number to test.
div $s1, $s0, 2 # $s1 is n / 2, the limit for the loop.
li $s2, 2 # $s2 is the counter starting at 2.
while: beq $s2, $s1, set
nop
remu $s3, $s0, $s2
beq $s3, $0, unset
nop
addiu $s2, $s2, 1
j while
nop
unset: li $s7, 0 # $s7 is unset if n isn't prime.
j end
nop
set: li $s7, 1 # $s7 is set because n is prime.
end:
The remu instruction is expanded to:
[00400040] 16400001 bne $18, $0, 4 ; 10: remu $s3, $s0, $s2
[00400044] 0000000d break
[00400048] 0212001b divu $16, $18
[0040004c] 00009810 mfhi $19
The break instruction is always stopping the code, even if $18 ($s2) is non-zero.

MIPS Mult two numbers and result it negative number

I am new for MIPS, there is my code:
.text
main:
add $s0, $zero, 1 # set $s0 to 0 for the initial value for the following loop
Loop: jal srand # Call function srand
addi $s0, $s0, 1 # $s0 = i++
slti $s1, $s0, 6 # $s1 < 6
bne $s1, $zero, Loop # go to loop in i < 6
beq $s1, 6, end_loop # go out the loop when i == 6
end_loop:
li $v0, 10
syscall
srand: # This function will set the numbers for future calculation
lw $t0, a # Load a value to $t0 1103515245
lw $t1, c # Load c value to $t1 12345
lw $t2, m # Load m value to $t2 2147483648
multu $t0,$s0 # result for multiplication (Xn*a) and store the result to $t3
add $t4, $t3, $t1 # result for add multiplication (Xn*a+c) and store the result to $t4
move $a0, $t4 # Debug function
li $v0, 1 # Debug function
syscall # Debug function
la $a0, msg
li $v0, 4
syscall
jr $ra
There is a problem, when the code goes to this "multu $t0,$s0" commend and result will be wrong.
1103515245 * 2 returned a negative number -2087936806
anyone know how to fix it ??
thanks
As chrylis said, it looks like integer overflow. If that's unclear to you, you should read up on two's complement integer representations.
Basically, the value of the highest-order bit is defined to be negative. Suppose you have 32-bit integers. Then 0x800000 will have the value of -2**32, and the other bits have their normal value, so you end up with an expression that looks like -2**32 + [the sum of the other bits], in particular 0xFFFFFFFF has the value of -1, 0xFFFFFE is -2, and so on.

MIPS MIDI Player: problems with playback and skip

I have a couple of questions about my group project. The first question is regarding playback of a MIDI file. Right now our program can playback a type 0 file with about 75% accuracy (if you can even gauge that). Basically, it get "most" of the song right (or really close). It's clearly not playing every sound and some of the ones it does play sound distorted. So if it's playing most of the notes correctly, what could cause it to miss or distort the others?
The second question is concerned with our interrupt handler. I'm trying to adjust the pointer so the program will skip X bytes forward/backward, but if I try the interrupt while the program is running it stalls and locks the program up. If I run through the interrupt step-by-step it won't stall. It seems like I'm either halfway there or totally off the mark (this was working on an earlier version, but probably b/c the playback function wasn't written correctly). If I had to guess I'd say the problem has something to do with the WITH_STATUS and WITHOUT_STATUS functions. Is that right?
Thank you in advance for any help.
PLAY_MIDI:
addi $s5, $0, 0 # temporaries to control speed and volume
addi $t6, $0, 100
addi $t9, $0, 0
addi $sp, $sp, -4 # add a byte to the stack pointer and store the return address there
sw $ra, 0($sp)
addi $t4, $0, 3
LOOP:
lh $t0, DAT_Format
bnez $t0, NO_SUPPORTED
addi $s3, $s3, 1
addi $s1, $s1, -1
lbu $t9, 0($s3) # look for status byte
blez $s1, OFF_LOOP
bltu $t9, 0x80, LOOP
bgeu $t9, 0xa0, LOOP
WITH_STATUS:
lbu $t9, 0($s3)
lb $t6, -1($s3) #sleep time
lb $a1, 1($s3) # load note
lb $a2, 2($s3) # load volume
addi $s3, $s3, 4
addi $s1, $s1, -4
move $s2, $s1 # *** copy $s1 to $s2 for pause/play
j SLEEP
WITHOUT_STATUS:
lb $t6, -1($s3) #sleep time
lb $a1, 0($s3) # load note
lb $a2, 1($s3) # load volume
addi $s3, $s3, 3 #increase stack
addi $s1, $s1, -3
move $s2, $s1 # ** copy $s1 to $s2 for pause/play
SLEEP:
mul $t6, $t6, $t4
li $v0, 32 # sleep until the next note should be played
addu $a0, $0, $t6 # sleep for $t6 ms
syscall
and $a0, $t9,0x0F # load channel
and $t9,$t9,0x000000F0 # load Command
######################################################
## copy $s1 to $s2 for use differentiating between ##
beq $t9, 0x90, PLAY_NOTE_ON ## stop and pause ##
beq $t9, 0x80, PLAY_NOTE_OFF ## $s2 will hold last position of $s1 ##
bgeu $t9, 0xa0, LOOP ######################################################
AFTER_PLAY:
blez $s1, OFF_LOOP
lbu $t0, 0($s3)
bgeu $t0, 0x80, WITH_STATUS
j WITHOUT_STATUS
OFF_LOOP:
bne $s1, $s2, PAUSE_LOOP # get the return address from the stack and decrease the pointer
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra # jump to the address we just loaded
PLAY_NOTE_ON:
li $v0, 60 # add a byte to the stack pointer and store the return address there
syscall
j AFTER_PLAY
PLAY_NOTE_OFF:
li $v0, 61
syscall
j AFTER_PLAY
NO_SUPPORTED:
j EXIT
#### Below is part of the interrupt handler ####
SKIP_FORWARD: addi $s3, $s3, 1 # by adding to $s3 program will move pointer ahead
la $a0, skip_forward # changed from $s3 to $s1 ***
li $v0, 4
syscall
jr $ra
SKIP_BACKWARD: addi $s3, $s3, -1 # by subtracting from $s3 program will move pointer backwards
la $a0, skip_backward # changed $s3 to $s1
li $v0, 4
syscall
jr $ra

MIPS debugging help

It seems that my understanding of MIPS fails me.
What I need to do is create a program that reverses a string input from terminal, using a stack, but without using $sp. Is there anyone here that knows MIPS?
Register usage
t0 - theString address start
t1 - stack address start
t2 - temp for retrieved character
t3 - counter for t0
t4 - counter for stack
t5 - newline
t6 - Length
.data
theString: .space 42
newLine: .asciiz "\n"
stack: .space 42
.globl main
.text
main:
li $v0, 8 # Set to read string
la $a0, theString
li $a1, 42 # Set size of string
syscall # Read string from terminal
la $t0, theString # Prepare t0 with theString
la $t1, stack # Prepare t1 with stack
la $t5, newLine
addi $t3, $t0, 0
addi $t4, $t1, 42
j push
push:
addi $t3, $t3, 1
addi $t4, $t4, -1
lb $t2, ($t3)
beq $t2, $t5, epush
sb $t2, ($t4)
j push
epush:
sub $t6, $t3, $t0
addi $t6, $t6, -1
addi $t3, $t0, 0
addi $t4, $t1, 0
j pop
pop:
addi $t3, $t3, 1
addi $t4, $t4, 1
lb $t2, ($t4)
beq $t2, $t5, epop
sb $t2, ($t3)
j pop
epop:
addi $t3, $t3, 1
sb $t5, ($t3)
li $v0, 4 # Set to print string
la $a0, theString # Set var to syscall output register
syscall # Print string
li $v0, 10 # Set to end program
syscall # End Program
For example, this just gives an infinite loop. (Sorry for lack of comments, I'm just tearing my hair out here)
Now, I think the problem is somewhere related to the newline character, but I don't know where?
Is there a reason you are using j and not jal? And it appears you are using SPIM, which has numerous issues.
In your pop loop, you are comparing (in beq) the address of newline (in $t5) and the address of the next character in the string. Even though they might contain the address of a location that contains "\n", the addresses might not be the same, because "\n" can be in more than one place. In fact, I can guarantee that they will never be the same, because one will refer to the address of the captured string, and the one in $t5 will be the address of the one at the beginning of the program.