Making a calculator in MIPS? - mips

I'm currently learning how to code using MIPS and the QTSPIM emulator. I thought making a simple arithmetic calculator would be good practice, so I started coding.
So far, the program asks for a number, reads the number, asks for an operation (user has to input either the operation symbols +,-,*,/ or the initials a,s,m,d). The next step would be to compare the received input with something...
This is the part I'm stuck at.
My first thought was to have 4 registers with the characters already in them, so I tried
li $t5,a
li $t6,s
li $t7,m
li $t8,d
That, however, just gives me a syntax error. I tried a couple other similar ways, but kept getting an error.
My objective was to compare the user input to those 4 registers and then move on to the operation
beq $t2,$t5,add
beq $t2,$t6,sub
beq $t2,$t7,mul
beq $t2,$t8,div
Where $t2 contains the character input by the user. It then branches off to the appropriate label to perform the operation.
Is there an easier way to do this that I'm overlooking?
Thanks for your time.

Well, I managed to solve my own problem XD
The solution is a bit primitive, but it works...
Changed the operation prompt to "1 for addition, 2 for subtraction, 3 for multiplication, 4 for division" and compared the user input with those,
beq $t2,1,add
beq $t2,2,sub
beq $t2,3,mul
beq $t2,4,div
And the rest is just the operations themselves.
Thought I'd leave this here just in case someone else runs into a similar problem.
Maybe I'll add memory and clear functions

To set a register with an ASCII code, you need to enclose it with single quotes.
In your example it would be
li $t5,'a'
li $t6,'s'
li $t7,'m'
li $t8,'d'

Related

MIPS Pseudoinstructions Dealing With li (big, small)

As a disclaimer, this is a for a school assignment, but I've been trying to understand this specific section of my homework for a few hours and I'm not finding any solution. I've googled, attempted to look up videos, read my book, etc.
I have a general gist of the normal MIPS instructions (still very much a beginner), but I'm very confused with some of the pseudoinstructions. We are asked to convert 10 or 12 pseudoinstructions to MIPS instructions.
I understand the simple pseudoinstructions, such as:
move &t1, $t2 # add $t1, $t2, $zero
But I'm very confused at ones involving "big," and "small," and "li"
li $t1, small (the hint we're given is "$t1 = small" which makes no sense to me, what is "small" in this context?).
Where I'm confused is, usually an immediate relates to a hard coded value, like 1 or 5, but I see no hard coded value. What am I supposed to be storing in $t1? I read that small means to 16 bit numbers and big is 32 bit, but what number are they referencing in this pseudoinstruction? There is only one register here and no hard value, what am I loading into $t1?
Thank you very much.

branch instructions, MIPS (using bare mode only)

I wonder if anyone knows if beq looks only at a specific bit (LSB) when branching to lable or does it look at the value of the entire register?
I am trying to implement multiplication routine
register t1 has 5(0x0000 0101) i am loading only a byte.
this instruction:
beq $t1,1, label -does not work for some reason.
when I go through the code step by step (i am using xspim -bare mode) it goes through the instruction and then just go to the next line of code when it should be skipping it because the lsb of $t1 is 1
Thanks !

What does the shift left logical single cycle datapath use?

I am asked to add the shift left logical instruction to a single cycle datapath. I know I need to feed the SHAMT field to the ALU, but I'm not sure how to do this. I understand the basics of single cycle data paths for R-format, branch, load word, and store word, but I'm not sure how the SLL plays in... Can anyone help explain how a single cycle SLL datapath works?
If this is the wrong form for this post, I'd be much obliged if someone would direct me to the correct site.
You simply need to decode the opcode of the SLL instruction and use it to set the ALUOp input of the ALU to 11. You also need to set the multiplexers to put the source register and the shift amount at the appropriate inputs to the ALU.

MIPS - JAL confusion: $ra = PC+4 or PC+8?

I'm having trouble understanding how the instruction jal works in the MIPS processor.
My two questions are:
a) What is the value stored in R31 after "jal": PC+4 or PC+8?
b) If it's really PC+8, what happens to the instruction at PC+4? Is it executed before the jump or is it never executed?
In Patterson and Hennessy (fourth edition), pg 113:
"jump-and-link instruction: An instruction that jumps to and address and simultaneously saves the address of the following instruction in a register ($ra in MIPS)"
"program counter (PC): The register containing the address of the instruction in the program being executed"
After reading those two statements, it follows that the value saved in $ra should be (PC+4).
However, in the MIPS reference data (green card) that comes with the book, the jal instruction's algorithm is defined like this:
"Jump and Link : jal : J : R[31]=PC+8;PC=JumpAddr"
This website also states that "it's really PC+8", but strangely, after that it says that since pipelining is an advanced topic "we'll assume the return address is PC+4".
I come from 8086 assembly, so I'm aware that there's a big difference between returning to an address and to the one following it, because programs won't work if I just assume something that's not true. Thanks.
The address in $ra is really PC+8. The instruction immediately following the jal instruction is in the "branch delay slot". It is executed before the function is entered, so it shouldn't be re-executed when the function returns.
Other branching instructions on the Mips also have branch delay slots.
The delay slot is used to do something useful in the time it takes to execute the jal instruction.
I got the same question. Googled this excellent answer of Richard and also another link I wish to add here.
The link is http://chortle.ccsu.edu/AssemblyTutorial/Chapter-26/ass26_4.html
with this wonderful explanation of double adding 4 to the PC.
So the actual execution has two additions: 1) newPC=PC+4 by pipelining and 2) another addition $ra=newPC+4 by the jal instruction resulting the effective $ra = (address of the jal instruction)+8.

Microprogramming in MIPS

I am learning about micro programming and am confused as to what a micro-instruction actually is. I am using the MIPS architecture. My questions are as follows
Say for example I have the ADD instruction, what would the micro-instructions look like for this? How many micro-instructions are there for the add instruction. Is there somewhere online I can see the list of micro-instructions for the basic instructions of MIPS?
How can I figure out the bit string for an ADD microprogrammed instruction?
Microprogramming is a method of implementing a complex instruction set architecture (such as x86) in terms of simpler "micro instructions". MIPS is a RISC instruction set architecture and is not typically implemented using micro-programming, so there are ZERO microinstructions for the ADD instruction.
To answer your specific question one would have to know what the definition of your particular micro-architecture is.
This is an example of how to load the EPC into one of the registers and add 4-bytes to it:
lw t0, 20(sp) // Load EPC
addi t0, 4 // Add 4 to the return adress
sw t0, 20(sp) // Save EPC
There are "a lot" of instructions that you can use, you can see the MIPS Instruction Set here. In my humble opinion, MIPS is Really neat and easy to learn! A fun fact is that the first Playstation used a MIPS CPU.
Example instructions
lw = load word
la = load address
sw = save word
addi = add immidate
Then you have a lot of conditional instructions such as:
bne = branch not equal
bnez = branch not equal zero
And with these you use j to jump to an adress.
Here is an example from an Exception Handler that I wrote once for MIPS, this is the External Source handler:
External:
mfc0 t0, C0_CAUSE // We could aswell use 24(sp) to load CAUSE
and t0, t0, 0x02000 // Mask the CAUSE
bnez t0, Puls // If the only character left is
// "not equal zero" jump to Puls
j DisMiss // Else jump to DisMiss
In the above example I define an entry point called External that I can jump to, as I do with DisMiss to loop, you generally jump to yourself.
There are some other instructions used here aswell:
mfc0 = move from co-processor 0
To handle labels, I would suggest you check this question/answer out.
Here's a couple of resources on MicroProgramming with MIPS:
Some general information
Here is a bit more heavy power-point presentation on the subject from Princton ( PDF )
Here is a paper from another university which is one of the best of these three ( PDF ).