Organization Lecture-4 Code Translation-2 Memory, Data transfer - - PowerPoint PPT Presentation

organization
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 1

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)

slide-2
SLIDE 2

Previously…

  • Registers

– $s0 - $s7, $t0 - $t9, $zero, $ra, $v0, $a0

  • Arithmetic and logical instructions

– add, sub, slt, mul, div – and, or, xor, nor, sll, srl, sra – the i and u suffix

  • lui
  • Jump and branch

– j, jr – beq, bne

  • I/O (syscall)
  • MIPS instruction formats

– R-type (register: e.g. add) – I-type (immediate: e.g. addi) – J-type (jump: e.g. j)

CSE-2021 2

slide-3
SLIDE 3

Memory Organization

  • Memory: large one dimensional

array of cells

  • Addresses are indices to the array
  • Each cell is 1 word (4 bytes) long
  • Each word in a memory has an

address, which is a multiple of 4 – min value of byte address = 0 – max value = 232 – 1

  • Data is transferred from memory

into registers using data transfer instructions

CSE-2021 3

100 10 101 1 12 8 4 Data Address Memory Processor

slide-4
SLIDE 4

The Load/Store Family (1)

  • Used to transfer data to/from DRAM

– to read/write .data, the heap, and the stack

  • lw and sw

– what are their algorithms?

  • lb and sb

– what does the u suffix do to lb?

CSE-2021 4

slide-5
SLIDE 5

The Load/Store Family (2)

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)

slide-6
SLIDE 6

The Load/Store Family (Example)

  • C instruction: g = h + A[k]
  • Register allocation:

– $s1 => g $s2 => h – $s3 contains base address

  • f array (i.e. address of A[0])

– $s4 contains value of k

MIPS Code:

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

slide-7
SLIDE 7

MIPS Fields for Data Transfer Operations (I-type)

  • Example: lw $t0,32($s3)

  • pcode = 3510 = (100011)2

– 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

  • p

rs rt address

6 bits 5 bits 5 bits 16 bits

  • pcode

1st operand 2nd operand Memory address (offset)

slide-8
SLIDE 8

Data Transfer Instructions (Exercise)

  • Write the MIPS assembly code for the

following C assignment instruction

– A[12] = h + A[8] – assuming that the variable h is stored in $s2 and the base address of the array A is in $s3

CSE-2021 8

slide-9
SLIDE 9

Assumptions for Phase II A Utility Class

  • A single class plus its client

– hence, two linked classes but no objects

  • Static Attributes only

– hence .data is needed but no heap

  • Can have several static methods

– hence stack is needed

CSE-2021 9

slide-10
SLIDE 10

Static Attributes and .data

  • To allocate static int x = 5 in .data:

x: .word* 5

  • To transfer the value of x to register s0:

lb/lh/lw $s0, x($0)

  • To transfer the value of register s0 to x:

sb/sh/sw $s0, x($0)

* can use byte/half/word, ascii/asciiz, or space

CSE-2021 10

slide-11
SLIDE 11

ASCII Characters

  • American Standard Code for Information

Interchange

– 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

  • Strings are sequences of characters + something

to represent its length

  • Three possible choices:

– end a string with a null character `\0’ (ANSI C) – use first byte to represent length (JAVA) – use accompanying variable to indicate length

  • How to load a 8 or 16 bits at a time?

– lb, sb, lh, sh

CSE-2021 11

slide-12
SLIDE 12

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

  • 45

= 61 M 77 ] 93 m 109 } 125 . 46 > 62 N 78 ^ 94 n 110 ~ 126 / 47 ? 63 O 79 _ 95

  • 111

DEL 127

CSE-2021 12

slide-13
SLIDE 13

.data Example #1

.data x: .byte 65 y: .word 123 z: .byte

  • 30

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

slide-14
SLIDE 14

.data Example #2

.data y: .word 123, 150, 22 z: .byte

  • 30, 12

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

slide-15
SLIDE 15

.data Example #3

.data x: .byte 65 y: .word 123, 150, 22 z: .byte

  • 30, 12

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

slide-16
SLIDE 16

Procedures (1)

  • Calling program:

– places parameters in registers $a0 - $a3 where the called procedure can access them – transfers control to the procedure (recall PC)

  • address of the next instruction in the calling program

is automatically placed in register $ra

  • jal proc

CSE-2021 16

Calling Program

Input Parameters $a0 - $a3 Output Parameters $v0 - $v1

Procedure

slide-17
SLIDE 17

Procedures (2)

  • Called Procedure:

– acquires additional space needed to perform the task

  • saves values of required registers in a stack $sp

– 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

slide-18
SLIDE 18

Procedures (Stack)

  • Where are values of the calling function stored?

– 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

  • f

r e g i s t e r $ s C o n t e n t s

  • f

r e g i s t e r $ t C o n t e n t s

  • f

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

  • w

a d d r e s s a . b . c .

$fp $fp $fp

slide-19
SLIDE 19

Stack Usage

  • To push the content of register $s0 on the stack:

addi $sp, $sp, -4 sw $s0, 0($sp)

  • To pop the word at the top of the stack into $s0:

lw $s0, 0($sp) addi $sp, $sp, 4

  • Every method must preserve $sp, $ra, and $sx

plus any other register it needs after a call

CSE-2021 19

slide-20
SLIDE 20
  • Proc. Example #1 (1)
  • Example:

int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; }

  • Assume: g, h, i, j are stored in registers $a0 - $a3
  • Calling function in MIPS:

main: … jal leaf_example …

CSE-2021 20

slide-21
SLIDE 21
  • Proc. Example #1 (2)

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

slide-22
SLIDE 22
  • Proc. Example #2 (1)
  • Nested Procedure Example:

int fact (int n)

{ if (n < 1) return (1) else return (n * fact(n-1)); }

  • Assume variable n is stored in $a0

CSE-2021 22

slide-23
SLIDE 23
  • Proc. Example #2 (2)
  • Before each function call

– value of $ra must be saved in the stack – value of $a0 must be saved in the stack

  • After returning from a function

– 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

slide-24
SLIDE 24
  • Proc. Example #2 (3)

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

slide-25
SLIDE 25
  • Proc. Example #2 (4):

Partial MIPS Code

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

slide-26
SLIDE 26
  • Proc. Example #2 (5):

MIPS Code

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

slide-27
SLIDE 27

Summary

  • Memory organization and load/store inst.
  • Static attributes and .data
  • ASCII & Unicode
  • Procedures

– recursive

  • Stack, $sp, $fp
  • What about linking and loading?

CSE-2021 27