Chapter 6 Introduction To MIPS Assembly Language
Chapter 6 Introduction To MIPS Assembly Language
Assembly Language
Programming
Dr Truong Dinh Huy
Previous Weeks
1
10/28/2024
Outline
• QtSPIM/MARS software.
• Register-Register instructions.
Assembly Language
2
10/28/2024
MIPS Architecture
MIPS – semiconductor company that built one of the
first commercial RISC architectures.
3
10/28/2024
• We will study the first MIPS model, the 32-bit MIPS R2000.
4
10/28/2024
Register divided
Instruction
into fields of bits
Decoders
Register
Result (to
Registers or
Registers
Decoders
Memory Memory)
Register
Instruction
Block ALU
Instr.
Decoders
Registers
• There are 32 registers in the MIPS processor.
• The MIPS R-2000 computer has 32-bit data words, so each register
has 32 flip-flops.
5
10/28/2024
20
• These registers are also generally used only for the purposes noted.
21
6
10/28/2024
23
7
10/28/2024
15
8
10/28/2024
17
9
10/28/2024
10
10/28/2024
Bit 31
Instruction fields The function code is the arithmetic/
op: operation code (opcode) (6 bits) logical function to be performed.
Example: Function code 10 0000 [0x20]
Rs: first source register number (5 bits) means “add.”
Rt: second source register number (5 bits)
Rd: destination register number (5 bits)
22
Sh Amt: shift amount (00000 for now) (5 bits)
Fun Code: function code (extends opcode)(6 bits)
11
10/28/2024
Register Block
$t1
0 17 18 8 0 32
000000100011001001000000001000002 = 0232402016
12
10/28/2024
Subtract
• Subtract has exactly the same form as add: sub rd, rs, rt.
Examples:
– sub $t3, $t4, $t5: [$t4] ‒[$t5] → [$t3].
– sub $s3, $s6, $t0: [$s6] ‒[$t0] → [$s3].
25
13
10/28/2024
Example
C code:
f = (g + h) - (i + j);
Assume that g in $t0, h in $t1, i in $t2, j in $t3, f in $t4. Compiled
MIPS code:
25
Multiply
• Multiply is written:
mult $t0, $t1 {$Hi, $Lo} = $t0*$t1
• Since multiplying two 32-bit numbers can have a 64-bit result, the
MIPS computer provides two 32-bit registers for a multiply result,
LO and HI .
• Multiply sends the upper 32 bits to HI and the lower 32 bits to LO.
• Note: You must check HI for a residual result (product > 32 bits).
SPIM does NOT do this!
• If we only care about the lower 32 bits of result:
mul $t2, $t0, $t1 $t2=$t0*$t1
26 Notes: The second operand for mul instruction can be a constant, as for addi.
14
10/28/2024
Divide
• Similarly, the MIPS divide is:
HI (32 bit remainder)
– div $t2,$t1, which means [$t2]/[$t1]→ LO (fixed point 32 bit quotient)
Logical Instructions
• Logical instructions perform logic functions on the arguments on
which they operate. For example:
– and $t0, $t1, $t2: [$t1]• [$t2] → [$t0].
• Logical operations include AND, OR, NOT, NOR, and XOR.
• Logical instructions are performed on the arguments on a bitwise
basis, as shown below (and $t0,$t1,$t2).
[$t1]= 0001110….0111
[$t2]= 0001110….1100
[$t0]= 0001110….0100
27
15
10/28/2024
Shift Operations
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
16
10/28/2024
AND Operations
Useful to mask bits in a word
Select some bits, clear others to 0
OR Operations
Useful to include bits in a word
Set some bits to 1, leave others unchanged
17
10/28/2024
NOT Operations
Pseudo Instructions
18
10/28/2024
“li” instructions may change the value of $at silently, that is why we don’t use
$at in our assembly program.
19
10/28/2024
16
17
20
10/28/2024
Program 2
• Write a program on your computer to do the
following:
– Enter 47 into $t0 (“x”)
– Put 253 into $t1 (“y”)
– Load 23 in $t2 (“z”)
– Compute x2 + 3y + 10z
– Store the result in $t3
– Stop the program by using system call 10.
– What is the number in $t3?
35
Program 3
37
21
10/28/2024
Program 4
• Write a program to solve the equation: w = 4x2+8y+z.
• Variable x is 0x123, y is 0x12b7, and z is 0x34af7.
• Use $t0 for x, $t1 for y, and $t2 for z.
• Remember to start your program with .text and label the
first instruction as “main:”.
• Store w in $s0 and end your program with syscall 10.
What is the value of w in hex?
• Try this program on your own
39
Summary
22
10/28/2024
Next week
23