Download as pdf or txt
Download as pdf or txt
You are on page 1of 94

ICT 1110 (2022/23)

Computer Systems & Architecture


Lecture 17: MIPS Instruction Set
Architecture II
Lighton Phiri <lighton.phiri@unza.zm>
Department of Library & Information Science
University of Zambia
https://bit.ly/3uJBZRc
Lecture Outline

● Introduction
● MIPS Instruction Formats
● R-Format Instructions
● I-Format Instructions
● J-Format Instructions
● Addition and Subtraction
● Multiplication
● Division
● MIPS System Calls
● Summary
October 4, 2023 ICT 1110 (2022/23) L17 - 2
Introduction (1/3)

http://rivoire.cs.sonoma.edu/cs351/wemips

● Other assemblers (software) might enforce syntactic rules


○ MipsIt (Standalone)
○ MARS (Standalone, with IDE)
○ WeMips (Web-based)
○ Qemu (hypervisor that supports MIPS)
October 4, 2023 ICT 1110 (2022/23) L17 - 3
Introduction (2/3)

● All MIPS instructions are encoded in binary


● All MIPS instructions have a fixed format
● All MIPS instructions are one word long
○ 1 word = 32 bits
○ 1 byte = 8 bits
○ 1 word = 4 bytes

00100001 00001001 11111000 00011101

October 4, 2023 ICT 1110 (2022/23) L17 - 4


Introduction (3/3)

● Machine code instructions are comprised of an operation


code and operands
○ Operation code—An instruction that performs an operation on
one or more input values, that produces a result
○ Operands—Input or output values used by an operation
● Operations and operands are binary codes that a computer
can interpret
○ The binary instructions control the hardware

Operation Code Operand

October 4, 2023 ICT 1110 (2022/23) L17 - 5


MIPS Instruction Formats (1/2)

● MIPS instructions are encoded in binary format.


● MIPS instructions are 32 bits long.
● MIPS has three instruction categories
○ R-format instructions
○ l-format instructions
○ J-format instructions
○ All instructions have the format
op (or opcode): operation code (specifies the operation) (first
6 bits)

October 4, 2023 ICT 1110 (2022/23) L17 - 6


MIPS Instruction Formats (2/2)
Segment Description # of bits Comment
opcode MIPS operations 6 R-, I- or J-format
funct Function code 6
rd — destination register
rd, rs, rt Register specifiers 5 rt — target register
rs — source register
Constant value to be used for
IMM Immediate value 16 arithmetic and logical
operation
shamt Shift amount 5 Number of bits to shift
target Memory address 26 Address in memory to jump to

October 4, 2023 ICT 1110 (2022/23) L17 - 7


MIPS Instruction Formats: R-Format (1/4)

● R-format instructions are used when all data values are


located in registers.
○ Take three arguments: two source registers (rs and rt) and one
destination register (rd).
opcode rd, rs, rt

rd Destination register

rs Source register #1

rt Source register #2

○ Example: add $t2, $t1, $t0 [which registers are these?]


October 4, 2023 ICT 1110 (2022/23) L17 - 8
MIPS Instruction Formats: R-Format (1/4)

● R-format instructions are used when all data values are


located in registers.
○ Take three arguments: two source registers (rs and rt) and one
destination register (rd).
opcode rd, rs, rt rd <- rs opcode rt
rd = rs opcode rt

rd Destination register

rs Source register #1

rt Source register #2

○ Example: add $t2, $t1, $t0 [which registers are these?]


October 4, 2023 ICT 1110 (2022/23) L17 - 9
MIPS Instruction Formats: R-Format (2/4)
opcode rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

● The opcode and operands are of fixed length of 32 bits.


○ opcode—Specific MIPS operation
○ rs—Input 32-bit register corresponding to the 32 (0—31) registers
○ rt—Input 32-bit register corresponding to the 32 (0—31) registers
○ rd—Output 32-bit register corresponding to the 32 (0—31)
registers
○ shamt—Shift amount used in shift instructions
○ funct—Used to indicate specific R-format instruction to use

October 4, 2023 ICT 1110 (2022/23) L17 - 10


MIPS Instruction Formats: R-Format (3/4)
opcode rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

● Allocating 5 bits to register values results in required range


for the 32 registers.
○ 0 ... 2n - 1 => 0 … 31
● The value held by the registers is independent of the register
number
○ Register number is between $0 … $31
○ Value held by registers is 1 word in size—32 bits
● opcode for R-format instructions is the same—0x00
○ The funct is used to indicate specific R-format instruction
October 4, 2023 ICT 1110 (2022/23) L17 - 11
MIPS Instruction Formats: R-Format (4/4)

● R-format instruction example: add $t2, $t1, $t0

opcode rs rt rd shamt funct


6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
add $t0 $t1 $t2
add $8 $9 $10 0x00 0x20
0x00 $8 $9 $10 0x00 0x20
000000 01000 01001 01010 00000 100000
000000 01000 01001 01010 00000 100000

October 4, 2023 ICT 1110 (2022/23) L17 - 12


MIPS Instruction Formats: R-Format (4/4)

● R-format instruction example: add $t2, $t1, $t0

opcode rs rt rd shamt funct


6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
add $t0 $t1 $t2
add $8 $9 $10 0x00 0x20
0x00 $8 $9 $10 0x00 0x20
000000 01000 01001 01010 00000 100000
000000 01000 01001 01010 00000 100000

October 4, 2023 ICT 1110 (2022/23) L17 - 13


MIPS Instruction Formats: R-Format (4/4)

● R-format instruction example: add $t2, $t1, $t0

opcode rs rt rd shamt funct


6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
add $t1 $t0 $t2
add $8 $9 $10 0x00 0x20
0x00 $8 $9 $10 0x00 0x20
$t1 = $9 $t0 = $8 $t2 = $10
000$t0 = $8
000000 01000 01001 01010 100000
00
000000 01000 01001 01010 00000 100000

October 4, 2023 ICT 1110 (2022/23) L17 - 14


MIPS Instruction Formats: R-Format (4/4)

● R-format instruction example: add $t2, $t1, $t0

opcode rs rt rd shamt funct


6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
add $t0 $t1 $t2
add $9 $8 $10 0x00 0x20
0x00 $8 $9 $10 0x00 0x20
000000 01000 01001 01010 00000funct =100000
shamt = 0x00 0x20
000000 01000 01001 01010 00000 100000

October 4, 2023 ICT 1110 (2022/23) L17 - 15


MIPS Instruction Formats: R-Format (4/4)

● R-format instruction example: add $t2, $t1, $t0

opcode rs rt rd shamt funct


6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
add $t0 $t1 $t2
add $9 $8 $10 0x00 0x20
0x00 $9 $8 $10 0x00 0x20
000000 01000 01001 01010 00000 100000
add = 0x00 000000 01000 01001 01010 00000 100000

October 4, 2023 ICT 1110 (2022/23) L17 - 16


MIPS Instruction Formats: R-Format (4/4)

● R-format instruction example: add $t2, $t1, $t0

opcode rs rt rd shamt funct


6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
add $t0 $t1 $t2
add $9 $8 $10 0x00 0x20
0x00 $9 $8 $10 0x00 0x20
000000 01001 01000 01010 00000 100000
000000 01000 01001 01010 00000 100000

October 4, 2023 ICT 1110 (2022/23) L17 - 17


MIPS Instruction Formats: R-Format (4/4)

● R-format instruction example: add $t2, $t1, $t0

opcode rs rt rd shamt funct


6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
add $t0 $t1 $t2
add $9 $8 $10 0x00 0x20
0x00 $9 $8 $10 0x00 0x20
000000 01001 01000 01010 00000 100000
add $t2, $t1, $t0 = 000000 01001 01000 01010 00000 100000

October 4, 2023 ICT 1110 (2022/23) L17 - 18


MIPS Instruction Formats: I-Format (1/4)

● I-format instructions are used when instructions need to


operate on an immediate value and a register value.
○ Takes three arguments: two registers (rs and rt) and one 16-bit
value (imm).
opcode rt, rs, imm

rt Target register

rs Source register

imm Immediate value

○ Example: addi $t1, $t0, 10


October 4, 2023 ICT 1110 (2022/23) L17 - 19
MIPS Instruction Formats: I-Format (1/4)

● I-format instructions are used when instructions need to


operate on an immediate value and a register value.
○ Takes three arguments: two registers (rs and rt) and one 16-bit
value (imm).
opcode rt, rs, imm rt <- rs opcode imm
rt = rs opcode imm

rd Destination register

rs Source register

imm Immediate value

○ Example: addi $t1, $t0, 10


October 4, 2023 ICT 1110 (2022/23) L17 - 20
MIPS Instruction Formats: I-Format (2/4)
opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits

● The opcode and operands are of fixed length of 32 bits.


○ opcode—Specific I-format MIPS operation
○ rs—Input 32-bit register corresponding to the 32 (0—31) registers
○ rt—Output 32-bit register corresponding to the 32 (0—31)
registers
○ IMM—Input 16-bit constant/value that is normalised using the
sign extend
○ To compensate for 16 bits, result is padded using MSB value
○ IMM range of values is -32768 … +32767

October 4, 2023 ICT 1110 (2022/23) L17 - 21


MIPS Instruction Formats: I-Format (3/4)
opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits

● The opcode and operands are of fixed length of 32 bits.


○ opcode is identified using predefined opcode
○ Unlike R-format instructions, the funct value is not applicable
○ rs and rt register numbers are represented using 5 bits
○ 0 ... 2n - 1 => 0 … 31
○ rs and rt register values held are represented using 32 bits
○ IMM value is stored using two’s complement
○ To derive decimal equivalent of IMM, follow conversion rules

October 4, 2023 ICT 1110 (2022/23) L17 - 22


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $8 $9 -2019
0x08 $8 $9 -2019
001000 01000 01001 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 23


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $8 $9 -2019
0x08 $8 $9 -2019
001000 01000 01001 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 24


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $zero $t0 -2019
addi $8 $0 -2019
0x08 $8 $0 -2019
$zero = $0 $8 = $8
001000 01000 00000 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 25


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $zero $t0 -2019
addi $0 $8 -2019
0x08 $8 $0 -2019
001000 01000 00000 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 26


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $t0 $zero -2019
addi $0 $8 -2019
0x08 $8 $0 -2019
addi 001000
= 0x08 01000 00000 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 27


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $t0 $zero -2019
addi $0 $8 -2019
0x08 $0 $8 -2019
001000 01000 00000 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 28


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $t0 $zero -2019
addi $0 $8 -2019
0x08 $0 $8 -2019
001000 01000 00000 1111100000011101
-2019 in 2’s complement using 16 bit representation
● 0000011111100011 (2019 in binary)
● 1111100000011100 (1’s complement)
● 1111100000011101 (add 1 to get 2’s complement)

October 4, 2023 ICT 1110 (2022/23) L17 - 29


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $t0 $zero -2019
addi $0 $8 -2019
0x08 $0 $8 -2019
001000 00000 01000 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 30


MIPS Instruction Formats: I-Format (4/4)

● I-format instruction example: addi $t0, $zero, -2019

opcode rs rt IMM
6 bits 5 bits 5 bits 16 bits
addi $zero $t0 -2019
addi $0 $8 -2019
0x08 $0 $8 -2019
001000 00000 01000 1111100000011101
addi $t0, $zero, -2019 = 001000 00000 01000 1111100000011101

October 4, 2023 ICT 1110 (2022/23) L17 - 31


MIPS Instruction Formats: J-Format (1/3)

● J-format instructions are used when a jump instruction has


to be performed. The control program flow.
○ Take one argument that represents the label to change program
control to.
opcode label1

label1 target address

○ Example
j loop1

October 4, 2023 ICT 1110 (2022/23) L17 - 32


MIPS Instruction Formats: J-Format (1/3)

● J-format instructions are used when a jump instruction has


to be performed. The control program flow.
○ Take one argument that represents the label to change program
control to.
opcode label1 rd <- rs opcode rt
rd = rs opcode rt

label1 target address

○ Example
j loop1

October 4, 2023 ICT 1110 (2022/23) L17 - 33


MIPS Instruction Formats: J-Format (2/3)
opcode target
6 bits 26 bits

● The opcode and target address are of fixed length of 32 bits.


○ opcode—Specific J-format MIPS operation
○ target—Input memory address to jump to

October 4, 2023 ICT 1110 (2022/23) L17 - 34


MIPS Instruction Formats: J-Format (3/3)

● J-format instruction example: j loop

opcode jump address


6 bits 26 bits
j loop
0x02 0x00400008
000010 00 0100 0000 0000 0000 0000 1000

October 4, 2023 ICT 1110 (2022/23) L17 - 35


MIPS Instruction Formats: J-Format (3/3)

● J-format instruction example: j loop

opcode jump address


6 bits 26 bits
j loop
0x02 0x00400008
000010 00 0100 0000 0000 0000 0000 1000

October 4, 2023 ICT 1110 (2022/23) L17 - 36


MIPS Instruction Formats: J-Format (3/3)

● J-format instruction example: j loop

opcode jump address


6 bits 26 bits
j loop
0x02 0x00400008
000010
j = 0x02 00 0100 0000loop
0000 0000address
“label” 0000 1000
dynamically determined

October 4, 2023 ICT 1110 (2022/23) L17 - 37


MIPS Instruction Formats: J-Format (3/3)

● J-format instruction example: j loop

opcode jump address


6 bits 26 bits
j loop
0x02 0x00400008
000010 00 0100 0000 0000 0000 0000 1000

October 4, 2023 ICT 1110 (2022/23) L17 - 38


MIPS Instruction Formats: J-Format (3/3)

● J-format instruction example: j loop

opcode jump address


6 bits 26 bits
j loop
0x02 0x00400008
000010 00 0100 0000 0000 0000 0000 1000
j loop = 00001000010000000000000000001000

October 4, 2023 ICT 1110 (2022/23) L17 - 39


MIPS Instruction Formats: Summary (1/1)
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
R
opcode rs rt rd shamt funct
6 bits 5 bits 5 bits 16 bits
I
opcode rs rt IMM
6 bits 26 bits
J
opcode target

October 4, 2023 ICT 1110 (2022/23) L17 - 40


MIPS Instructions (1/7)

Instr. Opcode Usage Format

add 0x00 Add R

addi 0x08 Add Immediate I

addiu 0x09 Add Unsigned Immediate I

and 0x00 Bitwise AND R

andi 0x0C Bitwise AND Immediate I

beq 0x04 Branch If Equal I

October 4, 2023 ICT 1110 (2022/23) L17 - 41


MIPS Instructions (2/7)

Instr. Opcode Usage Format

blez Homework Homework Homework

bne Homework Homework Homework

div Homework Homework Homework

divu Homework Homework Homework

j Homework Homework Homework

jal Homework Homework Homework

October 4, 2023 ICT 1110 (2022/23) L17 - 42


MIPS Instructions (3/7)

Instr. Opcode Usage Format

jr Homework Homework Homework

lbu Homework Homework Homework

lhu Homework Homework Homework

lui Homework Homework Homework

lw Homework Homework Homework

mfhi Homework Homework Homework

October 4, 2023 ICT 1110 (2022/23) L17 - 43


MIPS Instructions (4/7)

Instr. Opcode Usage Format

mflo Homework Homework Homework

mfc0 Homework Homework Homework

mul Homework Homework Homework

mulu Homework Homework Homework

nor Homework Homework Homework

xor Homework Homework Homework

October 4, 2023 ICT 1110 (2022/23) L17 - 44


MIPS Instructions (5/7)

Instr. Opcode Usage Format

or Homework Homework Homework

ori Homework Homework Homework

sb Homework Homework Homework

sh Homework Homework Homework

slt Homework Homework Homework

slti Homework Homework Homework

October 4, 2023 ICT 1110 (2022/23) L17 - 45


MIPS Instructions (6/7)

Instr. Opcode Usage Format

sltiu Homework Homework Homework

sltu Homework Homework Homework

sll Homework Homework Homework

srl Homework Homework Homework

sra Homework Homework Homework

sub Homework Homework Homework

October 4, 2023 ICT 1110 (2022/23) L17 - 46


MIPS Instructions (7/7)

Instr. Opcode Usage Format

subu Homework Homework Homework

sw Homework Homework Homework

October 4, 2023 ICT 1110 (2022/23) L17 - 47


Save Point (1/1)

● What is wrong with this program?


○ :
○ :
○ :

October 4, 2023 ICT 1110 (2022/23) L17 - 48


Arithmetic Operations: Addition (1/8)

● Adding two number


using a HLL involves
initialising variables
with number and
performing
mathematical
operation.

October 4, 2023 ICT 1110 (2022/23) L17 - 49


Arithmetic Operations: Addition (2/8)

● Load the value 5 to register 8


● Load the value 7 to register 9
● Add contents of register 8 and 9 and place the results into
register 10

October 4, 2023 ICT 1110 (2022/23) L17 - 50


Arithmetic Operations: Addition (3/8)

Notice the values of registers


8, 9 and 10.

October 4, 2023 ICT 1110 (2022/23) L17 - 51


Arithmetic Operations: Addition (4/8)

● Load the value 5 to register 8


● Add 7 to the contents of register 8

October 4, 2023 ICT 1110 (2022/23) L17 - 52


Arithmetic Operations: Addition (5/8)

Notice the value of register 8.

October 4, 2023 ICT 1110 (2022/23) L17 - 53


Arithmetic Operations: Addition (6/8)

● Operands must be
registers.
● Complex expressions need
to be broken down.

October 4, 2023 ICT 1110 (2022/23) L17 - 54


Arithmetic Operations: Addition (7/8)

● Complex expressions must be reduced to smallest possible


expressions. e.g. 5 + 2 + 7

October 4, 2023 ICT 1110 (2022/23) L17 - 55


Arithmetic Operations: Addition (8/8)

Notice the values of registers


involved.

October 4, 2023 ICT 1110 (2022/23) L17 - 56


Save Point (2)

● What registers will have their values changed?


● :
● :

October 4, 2023 ICT 1110 (2022/23) L17 - 57


Save Point (3)

● What registers will have their values changed?


● :
● :

October 4, 2023 ICT 1110 (2022/23) L17 - 58


Arithmetic Operations: Subtraction (1/3)

● Subtracting two number


using a HLL involves
initialising variables with
number and performing
mathematical operation
using ‘-’.

October 4, 2023 ICT 1110 (2022/23) L17 - 59


Arithmetic Operations: Subtraction (2/3)

● Subtracting is similar to addition, with the difference being


the operator: sub.

October 4, 2023 ICT 1110 (2022/23) L17 - 60


Arithmetic Operations: Subtraction (3/3)

Notice the values of registers


involved.

October 4, 2023 ICT 1110 (2022/23) L17 - 61


Arithmetic Operations: Multiplication (1/5)

● Multiplying two
number using a HLL
involves initialising
variables with
number and
performing
mathematical
operation using ‘*’.

October 4, 2023 ICT 1110 (2022/23) L17 - 62


Arithmetic Operations: Multiplication (2/5)

● Multiplication is typically performed using mul and mult.


○ mul rd, rs, rt

October 4, 2023 ICT 1110 (2022/23) L17 - 63


Arithmetic Operations: Multiplication (3/5)

Notice the values of registers


involved.

October 4, 2023 ICT 1110 (2022/23) L17 - 64


Arithmetic Operations: Multiplication (4/5)

● Multiplication is typically performed using mul and mult.


○ mult rs, rt

October 4, 2023 ICT 1110 (2022/23) L17 - 65


Arithmetic Operations: Multiplication (5/5)

Special register hi and lo have


values change.

October 4, 2023 ICT 1110 (2022/23) L17 - 66


Arithmetic Operations: Division (1/5)

● Dividing two number


using a HLL involves
initialising variables with
number and performing
mathematical operation
using ‘/’.

October 4, 2023 ICT 1110 (2022/23) L17 - 67


Arithmetic Operations: Division (2/5)

● Division is typically performed using mul and mult.


○ div rd, rs, rt
○ Result is place in rd

October 4, 2023 ICT 1110 (2022/23) L17 - 68


Arithmetic Operations: Division (3/5)

Special register hi and lo have


values change.

October 4, 2023 ICT 1110 (2022/23) L17 - 69


Arithmetic Operations: Division (4/5)

● Division operation results in a quotient and remainder.


○ div rs, rt
○ Quotient is placed in $hi register and remainder in $lo

October 4, 2023 ICT 1110 (2022/23) L17 - 70


Arithmetic Operations: Division (5/5)

Special register hi and lo have


values change.

October 4, 2023 ICT 1110 (2022/23) L17 - 71


Working with $hi and $lo Registers

● Contents of $hi and $lo registers, once modified by multiply


and divide instructions, can be accessed using mflo and mfhi
instructions.

● mflo rd
● mflo $8 will move contents of $lo into $8
● mfhi rd
● mfhi $9 will move contents of $hi into $9

October 4, 2023 ICT 1110 (2022/23) L17 - 72


System Calls (1/2)

● SPIM provides special instructions, called system calls, for


performing useful tasks and to transfer control to system
routines.
○ The syscall suspends execution of the program and then transfers
control to the operating system.
● Syscall codes determine the instruction to execute.
● $v0 contains the system call number to execute
○ Contents of $v0 are checked to determine the task requested by
the program.

October 4, 2023 ICT 1110 (2022/23) L17 - 73


System Calls (2/2)

● Syscalls are typically used to print data, read user input and
exiting programs.

Code Service Argument Result

1 Print integer $a0=value —

4 Print string $a0=string address —

5 Read integer — $v0=value

8 Read string $a0=str add; $a1=# char —

10 Exit — —

October 4, 2023 ICT 1110 (2022/23) L17 - 74


System Calls: Exiting

● Syscall 10 is used to graceful exit the program; this typically


means exiting ‘main’
○ Errors might arise if this is not done. E.g. Attempt to execute
non-instruction at 0x0040000c in QtSpim

October 4, 2023 ICT 1110 (2022/23) L17 - 75


System Calls: Printing Integers

● Syscall 1 enables the printing of integers.


○ Value to be printed must be place in $a0

October 4, 2023 ICT 1110 (2022/23) L17 - 76


System Calls: Integer Input

● Syscall 5 allows for integer value to be prompted as user


input to the program.
○ QtSpim prompt appears on the console.

October 4, 2023 ICT 1110 (2022/23) L17 - 77


System Calls: Printing Strings

● Syscall 4 enables the printing of strings—streams of


characters.
○ The string needs to be loaded to a memory address before use.

October 4, 2023 ICT 1110 (2022/23) L17 - 78


System Calls: String Input

● Syscall 8 enables for string input to be entered by users.


○ Memory needs to be allocated to hold the string
○ The length of characters needs to be specified

October 4, 2023 ICT 1110 (2022/23) L17 - 79


Example: Interactively Adding Two Integers
(1/5)
● Example: Prompt user for two integers and print the sum.
● STEP #1: Prompt first integer
● STEP #2: Prompt second integer
● STEP #3: Add two integers
● STEP #4: Print result

October 4, 2023 ICT 1110 (2022/23) L17 - 80


Example: Interactively Adding Two Integers
(2/5)
# Prompt user for two integers and print sum
.data
# Define variables to hold strings to print out
var_number1: .asciiz "Enter number 1: "
var_number2: .asciiz "Enter number 2: "
var_result: .asciiz "The sum of the two numbers is: "

.text
## STEP 1: Prompt first integer
# Print string for printing number 1
li $v0, 4
la $a0, var_number1
syscall

October 4, 2023 ICT 1110 (2022/23) L17 - 81


Example: Interactively Adding Two Integers
(3/5)
# Prompt for number 1 integer
li $v0, 5
syscall

add $8, $v0, $0 # move the number entered to register 8

## STEP 2: Prompt second integer


# Print string for printing number 1
li $v0, 4
la $a0, var_number2
syscall

October 4, 2023 ICT 1110 (2022/23) L17 - 82


Example: Interactively Adding Two Integers
(4/5)
# Prompt for number 2 integer
li $v0, 5
syscall

add $9, $v0, $0 # move the number entered to register 9

## STEP 3: Add the two numbers in registers 8 and 9

add $10, $8, $9

October 4, 2023 ICT 1110 (2022/23) L17 - 83


Example: Interactively Adding Two Integers
(5/5)
## STEP 4: Print the sum

li $v0, 4
la $a0, var_result
syscall

add $a0, $0, $10


li $v0, 1
syscall

## STEP 5: Exit program


li $v0, 10
syscall

October 4, 2023 ICT 1110 (2022/23) L17 - 84


Exercise: In-Class Exercises (1/3)

● Write an assembly language programs to evaluate the


following expressions
○ 1 + 2 + 3 + 4.
○ 1 - 2 - 3 - 4.
○ 1 + 2 - 3 + 4.
○ 2 * 3 - 4 / 2 + 8.

October 4, 2023 ICT 1110 (2022/23) L17 - 85


Exercise: In-Class Exercises (2/3)

● Write an assembly language program that prompts a user to


enter their name and, subsequently print out hello, followed
by the name they have entered. Prompt
○ "Please enter your full names "
○ Result "Hello, [FULL NAME]"

October 4, 2023 ICT 1110 (2022/23) L17 - 86


Exercise: In-Class Exercises (3/3)

● Write an assembly language program that prompts a user to


enter their date of birth (e.g. 2003) and then also prompt for
the current year (e.g. 2019). The program should then
calculate age of the user. Prompt 1 "Enter your date of birth
(e.g. 2003) : "
○ Prompt 2 "Enter the current year (e.g. 2018): "
○ Result "You are [AGE] years old."

October 4, 2023 ICT 1110 (2022/23) L17 - 87


MIPS System Calls (1/2)

Code Service Argument Result

1 Print integer $a0=value —

2 Print float $f12=value —

3 Print double $f12=value —

4 Print string $a0=string address —

5 Read integer — $v0=value

6 Read float — $f0=value

October 4, 2023 ICT 1110 (2022/23) L17 - 88


MIPS System Calls (2/2)

Code Service Argument Result

7 Read double — $f0=value

8 Read string $a0=str add; $a1=# char —

9 Memory allocation $s0=bytes of storage $v0 add

10 Exit — —

11 Print character $a0=integer —

12 Read character — $v0 char

October 4, 2023 ICT 1110 (2022/23) L17 - 89


Summary

● MIPS Instruction Formats


● R-Format Instructions
● I-Format Instructions
● J-Format Instructions
● Addition and Subtraction
● Multiplication
● Division
● MIPS System Calls

October 4, 2023 ICT 1110 (2022/23) L17 - 90


Q & A Session

● Comments, concerns and complaints?

October 4, 2023 ICT 1110 (2022/23) L17 - 91


Bibliography
[1] Patterson D. A. and Hennesy J.L. Computer Organisation and
Design. The Hardware/Software Interface. Chapter 2
[2] Patterson D. A. and Hennesy J.L. Computer Organisation and
Design. The Hardware/Software Interface. Appendix A
[3] Getting Started with SPIM
http://pages.cs.wisc.edu/~larus/spim.pdf
[4] MIPS Assembly Language Programming Using QtSpim
http://www.egr.unlv.edu/~ed/MIPStextSMv11.pdf
ict1110@unza.zm
https://bit.ly/3uJBZRc
http://bit.ly/2kK2ZkA
ICT 1110 (2022/23)
Computer Systems & Architecture
Lecture 17: MIPS Instruction Set
Architecture II
Lighton Phiri <lighton.phiri@unza.zm>
Department of Library & Information Science
University of Zambia
https://bit.ly/3uJBZRc

You might also like