Chapter 3 Data Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
Procedures & Data Representation - University of Arkansas
Transcript of Procedures & Data Representation - University of Arkansas
![Page 1: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/1.jpg)
Procedures & Data Representation
Alexander Nelson
February 1, 2021
University of Arkansas - Department of Computer Science and Computer Engineering
![Page 2: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/2.jpg)
Procedure – independent code module that
fulfills some concrete task, referenced within a
larger body of source code
0
![Page 3: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/3.jpg)
Supporting Procedures
Procedures allow programmer to focus on one portion of a task at
a time1
Steps Required:
1. Place parameters in registers
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call
1Slides copied and adapted from “Computer Organization and Design: The
Hardware/Software Interface” 5th edition instructor materials. Presented to
CSCE2214 according to fair use laws. Must attribute to original authors.
1
![Page 4: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/4.jpg)
Register Usage
32 MIPS registers:
• $a0-$a3 – Arguments (registers 4-7)
• $v0, $v1 – Result values (registers 2, 3)
• $t0-$t9 – Temporaries (can be overwritten by callee)
• $s0-$s7 – Saved (must be saved/restored by callee)
• $gp – Global pointer for static data (R28)
• $sp – Stack pointer (R29)
• $fp – Frame pointer (R30)
• $ra – Return address (R31)
2
![Page 5: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/5.jpg)
Procedure Call Instructions
Procedure call – Jump and link
jal ProcedureLabel
• Address of following instruction put in $ra
• Jumps to target address
Procedure return: jump register
jr $ra
• Copies $ra to program counter
• Can also be used for computed jumps (case/switch)
3
![Page 6: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/6.jpg)
Example – Leaf Procedure
Leaf Procedure – One that does not call another procedure
int leaf_example (int g, int h, int i, int j){
int f;
f = (g + h) - (i + j);
return f;
}
What would the compiled MIPS look like?
4
![Page 7: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/7.jpg)
Example – Leaf Procedure
Leaf Procedure – One that does not call another procedure
int leaf_example (int g, int h, int i, int j){
int f;
f = (g + h) - (i + j);
return f;
}
Assume Arguments, g, h, i, j in $a0 - $a3 respectively
f in $s0 (need to save $s0 on stack)
Result placed in $v0
5
![Page 8: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/8.jpg)
Example – Leaf Procedure
leaf_example:
addi $sp, $sp, -4 # increment stack pointer
sw $s0, 0($sp) # save $s0 on stack
add $t0, $a0, $a1 # begin procedure body
add $t1, $a2, $a3 #
sub $s0, $t0, $t1 # end procedure body
add $v0, $s0, $zero # store result into $v0
lw $s0, 0($sp) # restore $s0
addi $sp, $sp, 4 # pop off stack
jr $ra # vector back to return address
6
![Page 9: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/9.jpg)
Non-Leaf Procedures
Non-Leaf – Procedures that call other procedures
For nested call, caller needs to save:
• Return address
• Any arguments and temporaries needed after the call
Nested call needs to restore stack after call
7
![Page 10: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/10.jpg)
Example: Non-Leaf Procedure
int fact (int n){
if (n < 1) return 1;
else return n * fact(n - 1);
}
Assume argument in $a0
Result in $v0
How do we do this in assembly?
8
![Page 11: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/11.jpg)
Example: Non-Leaf Procedure
fact:
addi $sp, $sp, -8 # adjust stack for 2 items
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # save argument
slti $t0, $a0, 1 # test for n < 1
beq $t0, $zero, L1
addi $v0, $zero, 1 # if so, result is 1
addi $sp, $sp, 8 # pop 2 items from stack
jr $ra # and return
L1: addi $a0, $a0, -1 # else decrement n
jal fact # recursive call
lw $a0, 0($sp) # restore original n
lw $ra, 4($sp) # and return address
addi $sp, $sp, 8 # pop 2 items from stack
mul $v0, $a0, $v0 # multiply to get result
jr $ra # and return9
![Page 12: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/12.jpg)
Local Data on the Stack
Local data allocated by callee (e.g. C automatic variables)
Procedure Frame (Activation record)
• Used by some compilers to manage stack storage10
![Page 13: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/13.jpg)
Memory Layout
Text – program code
Static data – global variables
• static variables in C, constant
arrays/strings
• $gp initialized to address allowing
±offsets into this segment
Dynamic data – heap (e.g. malloc in C,
new in Java)
Stack – automatic storage
Von Neumann
Architecture
11
![Page 14: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/14.jpg)
Character Data
Byte-encoded character sets
• ASCII – 128 characters
95 graphic
33 control
• Latin-1 – 256 characters
ASCII + 96 more graphics
Unicode – 16- or 32-bit character set
• Used in Java, C++ wide characters
• Most of world’s alphabets + symbols
• UTF-8, UTF-16 – variable length encodings
More Description: Computerphile Video
12
![Page 15: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/15.jpg)
Byte/Halfword Operations
8-bit characters necessitate byte operations
Other data may require halfword operations Could use bitwise
operations to mask portions not to be changed
MIPS byte/halfword load/store
• lb – load byte
• lh – load halfword
• lbu – load byte unsigned
• lhu – load halfowrd unsigned
• sb – store byte
• sh – store halfword
Format lb rt, offset(rs) – Load byte into rt from rs with offset
Signed does sign extension, unsigned does zero pad
13
![Page 16: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/16.jpg)
String Copy Example
C Code (Not Safe!):
void strcpy (char x[], char y[]){
int i = 0;
while ((x[i]=y[i]) != ’\0’)
i += 1;
}
Addresses of x, y in $a0, $a1
i in $s0 – If leaf procedure, not necessary to be a saved variable
Why?
14
![Page 17: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/17.jpg)
String Copy Example
strcpy:
addi $sp, $sp, -4 # adjust stack for 1 item
sw $s0, 0($sp) # save $s0
add $s0, $zero, $zero # i = 0
L1: add $t1, $s0, $a1 # addr of y[i] in $t1
lbu $t2, 0($t1) # $t2 = y[i]
add $t3, $s0, $a0 # addr of x[i] in $t3
sb $t2, 0($t3) # x[i] = y[i]
beq $t2, $zero, L2 # exit loop if y[i] == 0
addi $s0, $s0, 1 # i = i + 1
j L1 # next iteration of loop
L2: lw $s0, 0($sp) # restore saved $s0
addi $sp, $sp, 4 # pop 1 item from stack
jr $ra # and return
15
![Page 18: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/18.jpg)
What about 32-bit constants?
Most constants are small – 16 bits sufficient
However, for the occasional 32-bit constant:
• lui rt, constant
• followed by ori rt, rs, constant
lui clears low 16-bits
lhi $s0, 61 0000 0000 0111 1101 0000 0000 0000 0000
ori $s0, $s0, 2304 0000 0000 0111 1101 0000 1001 0000 0000
16
![Page 19: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/19.jpg)
Branch Addressing
Branch instructions specify:
• opcode
• two registers
• target address
Most targets are near branch (either forward or backward)
Program Counter (PC)-relative addressing:
• Target Address = PC + offset x 4
• PC already incremented by 4 before this execution
17
![Page 20: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/20.jpg)
Jump Addressing
Jump (j/jal) targets may be anywhere in the text segment
Must encode full address in instruction:
(Pseudo)Direct jump addressing:
• Target Address = PC31−28|(address : ×4)
Why?
18
![Page 21: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/21.jpg)
Jump Addressing
Jump (j/jal) targets may be anywhere in the text segment
Must encode full address in instruction:
(Pseudo)Direct jump addressing:
• Target Address = PC31−28|(address : ×4)
2
2chortle.ccsu.edu/AssemblyTutorial/Chapter-17/ass17_5.html 19
![Page 22: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/22.jpg)
Target Addressing Example
Loop Example – Assume “Loop” at location 80000
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit: ...
20
![Page 23: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/23.jpg)
Branching Far Away
What if you need to branch further than 218 instructions away?
Example:
beq $s0, $s1, TARGET – Assume TARGET > 218 away
Assembler rewrites instruction as:
bne $s0, $s1, L2
j L1
L2: ...
Notice beq becomes a bne + jump – Gives 228 jump range
21
![Page 24: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/24.jpg)
Addressing Mode Summary
1. Immediate Addressing –
Operand is a constant within
instruction
2. Register Addressing – Operand
is the register
3. Base/displacement Addressing
– Operand is at memory
location, address is sum of
register+offset
4. PC-relative addressing – Branch
address is sum of PC and offset
5. Pseudodirect Addressing – Top
4 bits of PC + 26 bit constant
22
![Page 25: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/25.jpg)
Parallelism and Synchronization
Parallel Execution – Two or more tasks operating
at same time
Easier when tasks are independent
Data Race – Two memory accesses to same location from
different threads where one is a write
• P1 writes, P2 reads
• Result in P2 depends on order of these operations
Hardware Support needed
• Atomic read/write memory operation
• No other accesses to the location allowed between read and
write
23
![Page 26: Procedures & Data Representation - University of Arkansas](https://reader031.fdocuments.us/reader031/viewer/2022012300/61e144ae92d7cf408a775bc9/html5/thumbnails/26.jpg)
Synchronization in MIPS
Load Linked – ll rt, offset(rs)
Store Conditional – sc rt, offset(rs)
• Succeeds if location not changed since ll (returns 1 in rt)
• Fails if location is changed (returns 0 in rt)
Example: Atomic Swap (to test/set lock variable)
again: add $t0, $zero, 1 #copy exchange value
ll $t1, 0($s1) #load linked
sc $t0, 0($s1) #store conditional
beq $t0, $zero, again #branch store fails
add $s4, $zero, $t1 #put load value in $s4
Continues looping until ll/sc complete atomically
24