organization
play

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,


  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)

  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

  3. Memory Organization Memory: large one dimensional • array of cells Addresses are indices to the array • 12 100 Each cell is 1 word (4 bytes) long • 8 10 4 101 Each word in a memory has an • address, which is a multiple of 4 0 1 – min value of byte address = 0 Address Data – max value = 2 32 – 1 Processor Memory Data is transferred from memory • into registers using data transfer instructions CSE-2021 3

  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

  5. The Load/Store Family (2) Syntax Meaning Comments Example Memory to lw rt, imm(rs) rt = M[rs + SignExtImm] lw $s1, 100($s2) Register Register to sw rt, imm(rs) M[rs + SignExtImm] = rt sw $s1, 100($s2) memory Memory to lb rt, imm(rs) rt(7:0) = M[rs + SignExtImm](7:0) lb $s1, 100($s2) Register Register to sb rt, imm(rs) M[rs + SignExtImm](7:0) = rt(7:0) sb $s1, 100($s2) memory CSE-2021 5

  6. The Load/Store Family (Example) C instruction: g = h + A[k] • Register allocation: • A[k] 1 0 0 Address + 4xk $s1 => g $s2 => h – … 1 0 … $s3 contains base address – A[1] 1 0 1 address + 4 of array (i.e. address of A[0]) A[0] 1 address $s4 contains value of k – Array D a t a 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

  7. MIPS Fields for Data Transfer Operations (I-type) op rs rt address 6 bits 5 bits 5 bits 16 bits 1 st operand 2 nd operand opcode Memory address (offset) Example: lw $t0,32($s3) • opcode = 35 10 = (100011) 2 – rs = $s3 = 19 10 = (10011) 2 – rt = $t0 = 8 10 = (01000) 2 – address = 32 10 = (0000 0000 0010 0000) 2 – leads to the binary machine language code: – 100011 10011 01000 0000000000100000 CSE-2021 7

  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

  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

  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

  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

  12. ASCII ASCII ASCII ASCII ASCII ASCII Char Char Char Char Char Char value value value value value value space 0 @ P ` p 32 48 64 80 96 112 ! 1 A Q a q 33 49 65 81 97 113 “ 2 B R b r 34 50 66 82 98 114 # 3 C S c s 35 51 67 83 99 115 $ 4 D T d t 36 52 68 84 100 116 % 5 E U e u 37 53 69 85 101 117 & 6 F V f v 38 54 70 86 102 118 ‘ 7 G W g w 39 55 71 87 103 119 ( 8 H X h x 40 56 72 88 104 120 ) 9 I Y i y 41 57 73 89 105 121 * : J Z j z 42 58 74 90 106 122 + ; K [ k { 43 59 75 91 107 123 ‘ < L \ l | 44 60 76 92 108 124 - = M ] m } 45 61 77 93 109 125 . > N ^ n ~ 46 62 78 94 110 126 / ? O _ o DEL 47 63 79 95 111 127 CSE-2021 12

  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

  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

  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

  16. Procedures (1) Input Parameters $a0 - $a3 Calling Procedure Program Output Parameters $v0 - $v1 • 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

  17. Procedures (2) Input Parameters $a0 - $a3 Calling Procedure Program Output Parameters $v0 - $v1 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

  18. Procedures (Stack) H i g h a d d r e s s $fp $fp $ s p $ s p $fp C o n t e n t s o f r e g i s t e r $ t 1 C o n t e n t s o f r e g i s t e r $ t 0 $ s p C o n t e n t s o f r e g i s t e r $ s 0 L o w a d d r e s s a . b . c . • 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

  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

  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

  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

  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

  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

  24. Proc. Example #2 (3) Flow Diagram main $v0 = 2 x ($a0) 1 ($ra) 1 = return address in main = 6 ($a0) 1 = 3 fact # 1 ($ra) 2 = return address in fact # 1 $v0 = 1 x ($a0) 2 ($a0) 2 = 2 = 2 fact # 2 ($ra) 3 = return address in fact # 2 $v0 = 1 x ($a0) 3 ($a0) 3 = 1 = 1 fact # 3 CSE-2021 24

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend