Computer Organization

Computer Organization
Lab #2
Lab 2 Objectives
• Review on Lab 1
• System Calls
Some Instructions
Category
Instr. Meaning
Arithmetic
add
Add
add
$s1,$s2,$s3
-> $s1 = $s2 + $s3
sub
Subtract
sub
$s1,$s2,$s3
-> $s1 = $s2 - $s3
addi
Add
immediate
addi
$s1,$s2,20
-> $s1 = $s2 + 20
and
Bitwise And
and
$s1,$s2,$s3
-> $s1 = $s2 & $s3
or
Bitwise Or
or
$s1,$s2,$s3
-> $s1 = $s2 | $s3
nor
Bitwise Not
or
nor
$s1,$s2,$s3
-> $s1 = ~ ($s2 | $s3)
andi
Bitwise And
immediate
andi
$s1,$s2,20
-> $s1 = $s2 & 20
ori
Bitwise Or
immediate
ori
$s1,$s2,20
-> $s1 = $s2 | 20
sll
Shift left
logical
sll
$s1,$s2,10
-> $s1 = $s2 << 10
srl
Shift right
logical
srl
$s1,$s2,10
-> $s1 = $s2 >> 10
Logical
Example
Pseudo-Instructions
Category
Instr. Meaning
Example
Arithmetic
mul
Multiply
mul
$s1,$s2,$s3
-> $s1 = $s2 * $s3
div
Div
div
$s1,$s2,$s3
-> $s1 = $s2 / $s3
rem
Remainder
rem
$s1,$s2,$s3
-> $s1 = $s2 % $s3
Each instruction is encoded in exactly 1 word
New Easy Command
• Load Immediate:
li $s3, 5
#Stores 5 in $s3
Question -1?
• Assume $s0 = 1 and $s1=2
What is the final value for $s0 and $s1???
addi
add
sub
div
$s0,
$s0,
$s1,
$s1,
$s0,
$s1,
$s1,
$s0,
3
$s0
$s1
$s0
Hurry!!!!
Answer
Instruction
addi $s0,$s0,3
add
$s0,$s1,$s0
sub
$s1,$s1,$s1
div
$s1,$s0,$s0
$s0
4
6
6
6
$s1
2
2
0
1
Question-2?
• Reset the value of register $s3 by 3 different
instructions???
Hurry!!!!
Answer
Way 1:
sub $s3, $s3, $s3
Way 2:
li $s3,0
Way 3:
and $s3,$s3,$zero
Empty Program Skeleton
.data
#here we declare variables
.text
.globl
main:
#here we write the instructions
main
.end main
Exist System Call
.data
#here we declare variables
.text
.globl
main:
#here we write the instructions
main
li $v0, 10
syscall
.end main
Run & Debug
.data
#here we declare variables
.text #here we write the instructions
.globl main
main:
li $s0, 1
li $s1, 2
addi $s0,
add $s0,
sub $s1,
div $s1,
li
$v0,
syscall
.end main
$s0,
$s1,
$s1,
$s0,
10
3
$s0
$s1
$s0
Hurry!!!!
System Calls
Read $s0, $s1 from User
.data
#here we declare variables
.text #here we write the instructions
.globl main
main:
li
$v0, 5
# read int
syscall
#
V0 contains the result
add $s0, $0, $v0
li
$v0, 5
# read int
syscall
#
V0 contains the result
add $s1, $0, $v0
addi
add
sub
div
$s0,
$s0,
$s1,
$s1,
$s0,
$s1,
$s1,
$s0,
li
$v0, 10
syscall
.end main
3
$s0
$s1
$s0
Question-4?
• Modify the previous Code to Print the final
result for $s0 and $s1??
Hurry!!!!
Declaring Variables
• Data Types:
Declaring Variables
<variableName>:
.<dataType>
<initializationValue>
Declaring Variables
.data
#here we declare variables
first_num: .word 5
second_num: .word 10
sum_result: .word 0
.text #here we write the instructions
.globl main
main:
li
$v0, 10
syscall
.end main
Load/Store registers
.data
#here we declare variables
first_num: .word 5
second_num: .word 10
sum_result: .word 0
.text #here we write the instructions
.globl main
main:
lw $s0, first_num
lw $s1, second_num
add $s2, $s0, $s1
sw $s2, sum_result
li $v0, 10
syscall
.end main