Computer Organization Lab #3 Lab 3 Objectives • Review on previous labs • Memory Instructions – Variables – Load/store instructions Refresh your memory Integer Registers Register Name Register Number Register Usage Use in my programs? $zero $0 Constant 0 Yes $at $1 Reserved for the assembler No $v0 - $v1 $2 - $3 Function result (low/high) Yes $a0 - $a3 $4 - $7 Argument Register 1 Yes $t0 - $t7 $8 - $15 Temporary registers Yes $s0 - $s7 $16 - $23 Saved registers Yes $t8 - $t9 $24 - $25 Temporary registers Yes $k0 - $k1 $26 - $27 Reserved for OS kernel No $gp $28 Global variables pointer No $sp $29 Stack pointer Yes $fp $30 Frame pointer Yes $ra $31 Return address (used by function call) Yes Question? • We need 2 teams of 4 students • The first team to list 10 instructions wins!!! Hurry!!!! Some Instructions Category Instr. Meaning Example System syscall System call syscall 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 $s3) $s1,$s2,$s3 -> $s1 = ~ ($s2 | 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 Pseudo-Instructions Category Instr. Meaning 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 li Load Immediate Li $s1,10 -> $s1 = 10 Data Transfer Example Refresh your memory Empty Program Skeleton .data #here we declare variables .text .globl main: #here we write the instructions main .end main Refresh your memory System Calls MIPS Memory Instructions Registers & Memory • The ALU executes on data in registers $s1 $s2 ALU $s3 • Register size is a word (4 bytes), and we have a limited number of registers • So, How we deal with program data like variables, structures and arrays??? – We use the Memory Registers & Memory • Program data is stored in memory • Use Data Transfer Instructions: – Instructions to load the words from memory to registers – Instructions to store the words from registers back to memory Load word instruction: lw Memory R0 Store word instruction: sw Memory • Memory is a large single dimension array • The address of the word is the address of the lowest byte Memory • MIPS uses Little-Indian addressing: – The least significant byte is stored in the lowest memory address 5,000,000 (0x004C4B40) 42 (0x0000002A) 00 0x100100B 4C 0x100100A 4B 0x1001009 40 0x1001008 00 0x1001007 00 0x1001006 00 0x1001005 2A 0x1001004 Data Types Variable Value • The limits of the variable value depends on the variable size Size Number of different values allowed Unsigned Range Signed Range Byte (8 bits) 28 0 to 255 -128 to +127 Half(16 bits) 216 0 to 65,535 −32,768 to +32,767 0 to 4,294,967,295 −2,147,483,648 to +2,147,483,647 Word(32 bits) 232 Data Declarations • Variables: <variableName>: size: .word 10 .<dataType> <initializationValue> .<dataType> <value0>, <value1>,<value2> • Arrays: <arrayVarName>: list: .word 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 array1: .byte 'a','b' Data Declarations <variableName>: .<dataType> wVar1: .word 500000 wVar2: .word -100000 hVar1: .half 5000 hVar2: .half -3000 bVar1: .byte 5 bVar2: .byte -3 <initializationValue> Data Declarations <variableName>: pi: .<dataType> <initializationValue> .float 3.14159 tao: .double 6.28318 message1: .asciiz "Hello World\n" message2: .ascii "Line 1: Goodbye World\n" .ascii "Line 2: So, long and thanks " .ascii "for all the fish.\n" .asciiz "Line 3: Game Over.\n“ my_space: .space 1024 Load Address (la) Instruction la <target register> <variable or array> .data first_num: .word 5 list: .word 1,2,3,4 second_num: .word 10 .text .globl main main: la $s1, first_num la $s2, list la $s3, second_num .end main -> data segment base+0 -> data segment base+4 -> data segment base+20 Print Hello World .data my_msg: .asciiz "Hello World!\n" .text .globl main main: li $v0, 4 la $a0, my_msg syscall .end main Data Transfer Instructions • Load instruction format: <load instruction> <target register> <source memory address> Load Load Load Load Load word : lw half : lh half unsigned : lhu byte : lb byte unsigned : lbu variable name Or offset(base_register) • Store instruction format: <store instruction> <source register> <target memory address> Store word Store half Store byte : sw : sh : sb What is the difference between (lh & lhu) and (lb & lbu)? • Write and debug the below program to know the difference .data var1: .word -5000000 .text .globl main main: lw $s0, var1 lh $s1, var1 lhu $s2, var1 lb $s3, var1 lbu $s4, var1 .end main Hurry!!!! Hint: Try +ve and –ve values for var1 The Answer • The unsigned commands, ignore the sign and completes the register remaining bits by Zeros • The signed commands, completes the remaining bits with the sign value to keep the value original sign The Answer- Positive number • 5,000,000 = hex(0x004C4B40) var1 Instruction 5,000,000 lw lh lhu lb lbu Target Register 00 4c 4b 40 00 00 4b 40 00 00 4b 40 00 00 00 40 00 00 00 40 The Answer- Negative number • 5,000,000 = hex(0xffb3b4c0) var1 Instruction -5,000,000 lw lh lhu lb lbu Target Register ff b3 b4 ff ff b4 00 00 b4 ff ff ff 00 00 00 c0 c0 c0 c0 c0 Addition Program .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 Array addressing • Translate to MIPS: int a[3]; a[0] = 5; a[1] = 13; a[2] = 7; .data array1: .text main: la $t0 , li $t1 , sw $t1 , li $t1 , sw $t1 , li $t1 , sw $t1 , .end main .space 12 array1 5 ($t0) 13 4($t0) 7 8($t0)
© Copyright 2024 Paperzz