CSE 2021: Computer Organization
Lecture-4 Code Translation-2
Memory, Data transfer instructions, Data segment, ASCII, Procedures, Stack
Shakil M. Khan
(adapted from Prof. Roumani)
Organization Lecture-4 Code Translation-2 Memory, Data transfer - - PowerPoint PPT Presentation
CSE 2021: Computer Organization Lecture-4 Code Translation-2 Memory, Data transfer instructions, Data segment, ASCII, Procedures, Stack Shakil M. Khan (adapted from Prof. Roumani) Previously Registers $s0 - $s7, $t0 - $t9, $zero,
Lecture-4 Code Translation-2
Memory, Data transfer instructions, Data segment, ASCII, Procedures, Stack
(adapted from Prof. Roumani)
– $s0 - $s7, $t0 - $t9, $zero, $ra, $v0, $a0
– add, sub, slt, mul, div – and, or, xor, nor, sll, srl, sra – the i and u suffix
– j, jr – beq, bne
– R-type (register: e.g. add) – I-type (immediate: e.g. addi) – J-type (jump: e.g. j)
CSE-2021 2
array of cells
address, which is a multiple of 4 – min value of byte address = 0 – max value = 232 – 1
into registers using data transfer instructions
CSE-2021 3
100 10 101 1 12 8 4 Data Address Memory Processor
CSE-2021 4
CSE-2021 5
Syntax Meaning Comments Example lw rt, imm(rs) rt = M[rs + SignExtImm] Memory to Register lw $s1, 100($s2) sw rt, imm(rs) M[rs + SignExtImm] = rt Register to memory sw $s1, 100($s2) lb rt, imm(rs) rt(7:0) = M[rs + SignExtImm](7:0) Memory to Register lb $s1, 100($s2) sb rt, imm(rs) M[rs + SignExtImm](7:0) = rt(7:0) Register to memory sb $s1, 100($s2)
– $s1 => g $s2 => h – $s3 contains base address
– $s4 contains value of k
sll $t1,$s4,2 # $t1 = 4*k add $t1,$t1,$s3 # $t1 = address of A[0] + 4*k lw $t0,0($t1) # $t0 = A[k] add $s1,$s2,$t0 # $s3 = h + A[k]
CSE-2021 6 1 1 1 1 1 D a t a A[k] … A[1] A[0] Array address address + 4 … Address + 4xk
–
– rs = $s3 = 1910 = (10011)2 – rt = $t0 = 810 = (01000)2 – address = 3210 = (0000 0000 0010 0000)2 – leads to the binary machine language code: 100011 10011 01000 0000000000100000
CSE-2021 7
rs rt address
6 bits 5 bits 5 bits 16 bits
1st operand 2nd operand Memory address (offset)
CSE-2021 8
CSE-2021 9
CSE-2021 10
– 8 bits per char (in contrast to 16 bits unicode in Java) – e.g.: ASCII code for char C is (67)10 or (01000011)2
– end a string with a null character `\0’ (ANSI C) – use first byte to represent length (JAVA) – use accompanying variable to indicate length
– lb, sb, lh, sh
CSE-2021 11
Char ASCII value Char ASCII value Char ASCII value Char ASCII value Char ASCII value Char ASCII value space 32 48 @ 64 P 80 ` 96 p 112 ! 33 1 49 A 65 Q 81 a 97 q 113 “ 34 2 50 B 66 R 82 b 98 r 114 # 35 3 51 C 67 S 83 c 99 s 115 $ 36 4 52 D 68 T 84 d 100 t 116 % 37 5 53 E 69 U 85 e 101 u 117 & 38 6 54 F 70 V 86 f 102 v 118 ‘ 39 7 55 G 71 W 87 g 103 w 119 ( 40 8 56 H 72 X 88 h 104 x 120 ) 41 9 57 I 73 Y 89 i 105 y 121 * 42 : 58 J 74 Z 90 j 106 z 122 + 43 ; 59 K 75 [ 91 k 107 { 123 ‘ 44 < 60 L 76 \ 92 l 108 | 124
= 61 M 77 ] 93 m 109 } 125 . 46 > 62 N 78 ^ 94 n 110 ~ 126 / 47 ? 63 O 79 _ 95
DEL 127
CSE-2021 12
.data x: .byte 65 y: .word 123 z: .byte
u: .half 120 v: .ascii “York” s: .asciiz “York” t: .float 2.45 .text main: lb $t0, x($0) lw $t0, y($0) lb $t0, z($0) lbu $t0, z($0) lhu $t0, u($0) lbu $t0, v($0)
CSE-2021 13
.data y: .word 123, 150, 22 z: .byte
p: .word y .text main: addi $t1, $0, 8 lw $t0, y($t1) # index-like addi $t1, $0, 1 lb $t0, z($t1) # index-like la $t1, y lw $t0, 0($t1) # pointer-like lw $t0, 4($t1) # pointer-like lw $t1, p($0) addi $t1, $t1, 8 lw $t0, 0($t1) # pointer-like
CSE-2021 14
.data x: .byte 65 y: .word 123, 150, 22 z: .byte
u: .half 120 v: .ascii “York” s: .asciiz “York” p: .word y .text main: la $t1, x addi $t1, $t1, 8 lw $t0, 0($t1) # alignment! lw $t1, p($0) addi $t1, $t1, 16 # endianness lw $t0, 0($t1)
CSE-2021 15
– places parameters in registers $a0 - $a3 where the called procedure can access them – transfers control to the procedure (recall PC)
is automatically placed in register $ra
CSE-2021 16
Calling Program
Input Parameters $a0 - $a3 Output Parameters $v0 - $v1
Procedure
– acquires additional space needed to perform the task
– performs the desired task – restores the values of registers that it used – saves the result in registers $v0 - $v1 – returns control to the calling program by returning to instruction whose address is saved in $ra (jr $ra)
CSE-2021 17
Calling Program
Input Parameters $a0 - $a3 Output Parameters $v0 - $v1
Procedure
– a stack (assigned space) in the memory is used – register $sp contains the address of the stack pointer
CSE-2021 18
C o n t e n t s
r e g i s t e r $ s C o n t e n t s
r e g i s t e r $ t C o n t e n t s
r e g i s t e r $ t 1 $ s p $ s p $ s p
H i g h a d d r e s s L
a d d r e s s a . b . c .
$fp $fp $fp
addi $sp, $sp, -4 sw $s0, 0($sp)
lw $s0, 0($sp) addi $sp, $sp, 4
CSE-2021 19
int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; }
main: … jal leaf_example …
CSE-2021 20
leaf_example: # save registers $t0, $t1, $s1 addi $sp, $sp, -12 sw $t1, 8($sp) sw $t0, 4($sp) sw $s0, 0 ($sp) # perform the required operation add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0,$zero # restore registers $t0, $t1, $s1 lw $s0, 0 ($sp) lw $t0, 4($sp) lw $t1, 8($sp) addi $sp, $sp, 12 # return control jr $ra
CSE-2021 21
CSE-2021 22
– value of $ra must be saved in the stack – value of $a0 must be saved in the stack
– value of $a0 must be recalled for ($v0 × $a0) – value of $ra must be restored to return control to the calling function
CSE-2021 23
CSE-2021 24
main fact # 1 fact # 2 fact # 3 ($ra)1 = return address in main ($a0)1 = 3 ($ra)2 = return address in fact # 1 ($a0)2 = 2 ($ra)3 = return address in fact # 2 ($a0)3 = 1 $v0 = 1 x ($a0)3 = 1 $v0 = 2 x ($a0)1 = 6 $v0 = 1 x ($a0)2 = 2
Flow Diagram
fact: # input value is stored in $a0 and output value in $v0 # add commands for saving $ra, $a0 addi $t1, $zero, 1 # initialize $t1 = 1 slt $t0, $a0, $t1 # if (n < 1), $t0 = 1 beq $t0, $zero, L1 # if $t0 == 0, go to L1 # if n < 1 addi $v0, $zero,1 # return 1 jr $ra # if n >= 1 L1: addi $a0, $a0, -1 # $a0 = $a0 -1 jal fact # add commands to restore $ra, $a0 # process & return answer in $v0
CSE-2021 25
fact: addi $sp, $sp, -8 # clear stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save n addi $t1, $zero, 1 # initialize $t1 = 1 slt $t0, $a0, $t1 # if (n < 1), $t0 = 1 beq $t0, $zero, L1 # if $t0 == 0, go to L1 # if n < 1 add $v0,$zero,1 # return 1 addi $sp, $sp, 8 jr $ra # if n >= 1 L1: addi $a0, $a0, -1 # $a0 = $a0 -1 jal fact lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, -8 mul $v0,$a0,$v0 # pseudo-instruction (li, la, move, blt, bge, …) jr $ra
CSE-2021 26
CSE-2021 27