Reputation: 21
So here is the assignment and then below that are my questions and code I have so far.
Write a program that loads four signed integer operands (A, B, C, and D) from memory into registers,
shifts and rotates instructions to multiply the value of A by 5,
divide the value of B by 4 (truncating the result),
then shifts the value of C to the right 3 bit places and
rotates the value of D 2 bit places to the left.
Finally, the program will write the new values of A, B, C, and D back to memory.
Be sure to test your program carefully, using both positive and negative integers for inputs. Validate correct program operation by examining the updated contents of the memory operands. Do NOT use multiply or divide instructions! (HINT: 5A = 4A + A.)
Please include C code for each line if possible. Thanks!
I can not figure out how to multiply by an odd number, I know I need to multiply to get as close as I can an add 1, but I dont cant figure out how to do that.
This code I will paste below which I have so far works for B, C and D....for both positive and negative numbers, however in the I/O section Mars prints the result I ask for then says -- program is finished running (dropped off bottom) --. What does that mean and how do I fix it.
I am not sure B is actually truncating the result, please confirm or correct and explain. Thanks.....below is my code so far.
.text
main:
lw $t0, A
lw $t1, B
lw $t2, C
lw $t3, D
sll $s0, $t0, 2 # A= 5*4=20
sra $s1, $t1, 2 # B= -44/4=-11
sra $s2, $t2, 3 # C= -128/8=-16
rol $s3, $t3, 2 # D= -8 becomes -29
li $v0, 1
move $a0, $s3 # system call to print and check results
syscall
.data # the .data directive starts the data section
A: .word 5
B: .word -44
C: .word -128
D: .word -8
li $v0, 10 # exit
syscall
Upvotes: 2
Views: 2297
Reputation: 11
For your second question, where you said the program says "however in the I/O section Mars prints the result I ask for then says -- program is finished running (dropped off bottom) --. What does that mean and how do I fix it."
Understand that MARS (Compiler used to run the MIPS instructions) runs the whole code in a single direction fashion, starts reading from the .text section and keeps going downwards till it finds no more code to read. If not exited properly, it like drops off from the last line of code and exits in a no so graceful fashion.
In your case you have your syscall 10 instruction in the data section. Put the syscall 10 instruction in the .text section, that is right above your .data section so that program doesn't drop off the bottom. Hope that helped
Upvotes: 1
Reputation: 28931
After the sll $s0, $t0, 2 , $s0 = 20, not 25, since it's $t0 << 2. Just like the hint states, you need to do an add as well.
Upvotes: 2