Control Flow and Arrays COE 301 Computer Organization
- Slides: 37
Control Flow and Arrays COE 301 Computer Organization Prof. Aiman El-Maleh College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals [Adapted from slides of Dr. M. Mudawar, COE 301, KFUPM]
Presentation Outline v Control Flow: Branch and Jump Instructions v Translating If Statements and Boolean Expressions v Arrays v Load and Store Instructions v Translating Loops and Traversing Arrays v Addressing Modes Control Flow and Arrays COE 301 – KFUPM slide 2
Control Flow v High-level programming languages provide constructs: ² To make decisions in a program: IF-ELSE ² To repeat the execution of a sequence of instructions: LOOP v The ability to make decisions and repeat a sequence of instructions distinguishes a computer from a calculator v All computer architectures provide control flow instructions v Essential for making decisions and repetitions v These are the conditional branch and jump instructions Control Flow and Arrays COE 301 – KFUPM slide 3
MIPS Conditional Branch Instructions v MIPS compare and branch instructions: beq Rs, Rt, label if (Rs == Rt) branch to label bne Rs, Rt, label if (Rs != Rt) branch to label v MIPS compare to zero & branch instructions: Compare to zero is used frequently and implemented efficiently bltz Rs, label if (Rs < 0) branch to label bgtz Rs, label if (Rs > 0) branch to label blez Rs, label if (Rs <= 0) branch to label bgez Rs, label if (Rs >= 0) branch to label v beqz and bnez are defined as pseudo-instructions. Control Flow and Arrays COE 301 – KFUPM slide 4
Branch Instruction Format v Branch Instructions are of the I-type Format: Op 6 Rs 5 Rt 5 16 -bit offset Instruction beq bne blez bgtz bltz bgez Rs, Rs, Rs, Rt, label label I-Type Format Op = 4 Op = 5 Op = 6 Op = 7 Op = 1 Rs Rs Rs Rt Rt 0 0 0 1 16 -bit 16 -bit Offset Offset v The branch instructions modify the PC register only v PC-Relative addressing: If (branch is taken) PC = PC + 4×offset else PC = PC+4 Control Flow and Arrays COE 301 – KFUPM slide 5
Unconditional Jump Instruction v The unconditional Jump instruction has the following syntax: j label. . . # jump to label: v The jump instruction is always taken v The Jump instruction is of the J-type format: Op 6 = 2 26 -bit address v The jump instruction modifies the program counter PC: PC 4 26 -bit address v The upper 4 bits of the PC are unchanged Control Flow and Arrays COE 301 – KFUPM 00 multiple of 4 slide 6
Translating an IF Statement v Consider the following IF statement: if (a == b) c = d + e; else c = d – e; Given that a, b, c, d, e are in $t 0 … $t 4 respectively v How to translate the above IF statement? bne $t 0, $t 1, else addu $t 2, $t 3, $t 4 j next else: subu $t 2, $t 3, $t 4 next: . . . Control Flow and Arrays COE 301 – KFUPM slide 7
Logical AND Expression v Programming languages use short-circuit evaluation v If first condition is false, secondition is skipped if (($t 1 > 0) && ($t 2 < 0)) {$t 3++; } # One Possible Translation. . . bgtz $t 1, L 1 # first condition j next # skip if false $t 2, L 2 # secondition next # skip if false L 1: bltz j L 2: addiu $t 3, 1 # both are true next: Control Flow and Arrays COE 301 – KFUPM slide 8
Better Translation of Logical AND if (($t 1 > 0) && ($t 2 < 0)) {$t 3++; } Allow the program to fall through to secondition !($t 1 > 0) is equivalent to ($t 1 <= 0) !($t 2 < 0) is equivalent to ($t 2 >= 0) Number of instructions is reduced from 5 to 3 # Better Translation. . . blez $t 1, next # 1 st condition false? bgez $t 2, next # 2 nd condition false? addiu $t 3, 1 # both are true next: Control Flow and Arrays COE 301 – KFUPM slide 9
Logical OR Expression v Short-circuit evaluation for logical OR v If first condition is true, secondition is skipped if (($t 1 > 0) || ($t 2 < 0)) {$t 3++; } v Use fall-through to keep the code as short as possible bgtz $t 1, L 1 # 1 st condition true? bgez $t 2, next # 2 nd condition false? L 1: addiu $t 3, 1 # increment $t 3 next: Control Flow and Arrays COE 301 – KFUPM slide 10
Compare Instructions v MIPS also provides set less than instructions slt Rd, Rs, Rt if (Rs < Rt) Rd = 1 else Rd = 0 sltu Rd, Rs, Rt unsigned < slti Rt, Rs, imm if (Rs < imm) Rt = 1 else Rt = 0 sltiu Rt, Rs, imm unsigned < v Signed / Unsigned comparisons compute different results Given that: $t 0 = 1 and $t 1 = -1 = 0 xffff slt $t 2, $t 0, $t 1 computes $t 2 = 0 sltu $t 2, $t 0, $t 1 computes $t 2 = 1 Control Flow and Arrays COE 301 – KFUPM slide 11
Compare Instruction Formats Instruction Meaning Format slt Rd, Rs, Rt Rd=(Rs <s Rt)? 1: 0 Op=0 Rs Rt Rd 0 0 x 2 a sltu Rd, Rs, Rt Rd=(Rs <u Rt)? 1: 0 Op=0 Rs Rt Rd 0 0 x 2 b slti Rt, Rs, im Rt=(Rs <s im)? 1: 0 0 xa Rs Rt 16 -bit immediate sltiu Rt, Rs, im Rt=(Rs <u im)? 1: 0 0 xb Rs Rt 16 -bit immediate v The other comparisons are defined as pseudo-instructions: seq, sne, sgtu, sleu, sgeu Pseudo-Instruction sgt seq Control Flow and Arrays Equivalent MIPS Instructions $t 2, $t 0, $t 1 slt $t 2, $t 1, $t 0 $t 2, $t 0, $t 1 subu sltiu $t 2, $t 0, $t 1 $t 2, 1 COE 301 – KFUPM slide 12
Pseudo-Branch Instructions v MIPS hardware does NOT provide the following instructions: blt, ble, bgt, bge, bltu bleu bgtu bgeu branch if less than (signed / unsigned) branch if less or equal (signed / unsigned) branch if greater than (signed / unsigned) branch if greater or equal (signed / unsigned) v MIPS assembler defines them as pseudo-instructions: Pseudo-Instruction blt ble Equivalent MIPS Instructions $t 0, $t 1, label slt bne $at, $t 0, $t 1 $at, $zero, label $t 0, $t 1, label slt beq $at, $t 1, $t 0 $at, $zero, label $at ($1) is the assembler temporary register Control Flow and Arrays COE 301 – KFUPM slide 13
Using Pseudo-Branch Instructions v Translate the IF statement to assembly language v $t 1 and $t 2 values are unsigned if($t 1 <= $t 2) { $t 3 = $t 4; } bgtu move L 1: $t 1, $t 2, L 1 $t 3, $t 4 v $t 3, $t 4, and $t 5 values are signed if (($t 3 <= $t 4) && ($t 4 >= $t 5)) { $t 3 = $t 4 + $t 5; } Control Flow and Arrays bgt blt addu L 1: COE 301 – KFUPM $t 3, $t 4, L 1 $t 4, $t 5, L 1 $t 3, $t 4, $t 5 slide 14
Conditional Move Instructions Instruction Meaning R-Type Format movz Rd, Rs, Rt if (Rt==0) Rd=Rs Op=0 Rs Rt Rd 0 0 xa movn Rd, Rs, Rt if (Rt!=0) Rd=Rs Op=0 Rs Rt Rd 0 0 xb if ($t 0 == 0) {$t 1=$t 2+$t 3; } else {$t 1=$t 2 -$t 3; } bne addu j L 1: subu L 2: . . . $t 0, $0, L 1 $t 1, $t 2, $t 3 L 2 $t 1, $t 2, $t 3 addu $t 1, $t 2, $t 3 subu $t 4, $t 2, $t 3 movn $t 1, $t 4, $t 0. . . v Conditional move can eliminate branch & jump instructions Control Flow and Arrays COE 301 – KFUPM slide 15
Next. . . v Control Flow: Branch and Jump Instructions v Translating If Statements and Boolean Expressions v Arrays v Load and Store Instructions v Translating Loops and Traversing Arrays v Addressing Modes Control Flow and Arrays COE 301 – KFUPM slide 16
Arrays v In a high-level programming language, an array is a homogeneous data structure with the following properties: ² All array elements are of the same type and size ² Once an array is allocated, its size cannot be modified ² The base address is the address of the first array element ² The array elements can be indexed ² The address of any array element can be computed v In assembly language, an array is just a block of memory v In fact, all objects are simply blocks of memory v The memory block can be allocated statically or dynamically Control Flow and Arrays COE 301 – KFUPM slide 17
Static Array Allocation v An array can be allocated statically in the data segment v A data definition statement allocates static memory: label: . type value 0 [, value 1. . . ] label: is the name of the array. type directive specifies the size of each array element value 0, value 1. . . specify a list of initial values v Examples of static array definitions: arr 1: . half 20, -1 # array of 2 half words arr 2: . word 1: 5 # array of 5 words (value=1) arr 3: . space 20 # array of 20 bytes str 1: . asciiz "Null-terminated string" Control Flow and Arrays COE 301 – KFUPM slide 18
Watching Values in the Data Segment v The labels window is the symbol table ² Shows labels and corresponding addresses v The la pseudo-instruction loads the address of any label into a register Control Flow and Arrays COE 301 – KFUPM slide 19
Dynamic Memory Allocation v One of the functions of the OS is to manage memory v A program can allocate memory on the heap at runtime v The heap is part of the data segment that can grow at runtime v The program makes a system call ($v 0=9) to allocate memory. text. . . li $a 0, 100 # $a 0 = number of bytes to allocate li $v 0, 9 # system call 9 syscall # allocate 100 bytes on the heap move $t 0, $v 0 # $t 0 = address of allocated block . . . Control Flow and Arrays COE 301 – KFUPM slide 20
Allocating Dynamic Memory on the Heap 0 x 7 fffffff Stack Segment Heap Area 0 x 10040000 0 x 10000000 Data Segment Static Area Text Segment 0 x 00400000 0 x 0000 Control Flow and Arrays Reserved COE 301 – KFUPM slide 21
Computing the Addresses of Elements v In a high-level programming language, an array is indexed array[0] is the first element in the array[i] is the element at index i &array[i] is the address of the element at index i &array[i] = &array + i × element_size v For a 2 D array, the array is stored linearly in memory matrix[Rows][Cols] has (Rows × Cols) elements &matrix[i][j] = &matrix + (i×Cols + j) × element_size v For example, to allocate a matrix[10][20] of integers: matrix: . word 0: 200 # 200 words (initialized to 0) &matrix[1][5] = &matrix + (1× 20 + 5)× 4 = &matrix + 100 Control Flow and Arrays COE 301 – KFUPM slide 22
Element Addresses in a 2 D Array Address calculation is essential when programming in assembly COLS 0 1 … j … COLS-1 0 ROWS 1 … i … ROWS-1 &matrix[i][j] = &matrix + (i×COLS + j) × Element_size Control Flow and Arrays COE 301 – KFUPM slide 23
Load and Store Instructions v Instructions that transfer data between memory & registers v Programs include variables such as arrays and objects v These variables are stored in memory v Load Instruction: load ² Transfers data from memory to a register Memory Registers store v Store Instruction: ² Transfers data from a register to memory v Memory address must be specified by load and store Control Flow and Arrays COE 301 – KFUPM slide 24
Load and Store Word v Load Word Instruction (Word = 4 bytes in MIPS) lw Rt, imm(Rs) # Rt MEMORY[Rs+imm] v Store Word Instruction sw Rt, imm(Rs) # Rt MEMORY[Rs+imm] v Base / Displacement addressing is used ² Memory Address = Rs (base) + Immediate (displacement) ² Immediate 16 is sign-extended to have a signed displacement Base or Displacement Addressing Op 6 Rs 5 Rt 5 immediate 16 + Memory Word Base address Control Flow and Arrays COE 301 – KFUPM slide 25
Example on Load & Store v Translate: A[1] = A[2] + 5 (A is an array of words) v Given that the address of array A is stored in register $t 0 lw $t 1, 8($t 0) # $t 1 = A[2] addiu $t 2, $t 1, 5 # $t 2 = A[2] + 5 sw $t 2, 4($t 0) # A[1] = $t 2 v Index of A[2] and A[1] should be multiplied by 4. Why? Memory Registers . . . $t 0 $t 1 $t 2 &A A[2] + 5. . . lw sw A[3] A[2] A[1] A[0] &A + 12 &A + 8 &A + 4 &A . . . Control Flow and Arrays COE 301 – KFUPM slide 26
Load and Store Byte and Halfword v The MIPS processor supports the following data formats: ² Byte = 8 bits, Half word = 16 bits, Word = 32 bits v Load & store instructions for bytes and half words ² lb = load byte, lbu = load byte unsigned, sb = store byte ² lh = load half, lhu = load half unsigned, sh = store halfword v Load expands a memory value to fit into a 32 -bit register v Store reduces a 32 -bit register value to fit in memory 32 -bit Register Control Flow and Arrays s sign – extend s s b 0 zero – extend 0 bu s sign – extend s s h 0 zero – extend 0 hu COE 301 – KFUPM slide 27
Load and Store Instructions Instruction Meaning I-Type Format lb Rt, imm(Rs) Rt 1 MEM[Rs+imm] 0 x 20 Rs Rt 16 -bit immediate lh Rt, imm(Rs) Rt 2 MEM[Rs+imm] 0 x 21 Rs Rt 16 -bit immediate lw Rt, imm(Rs) Rt 4 MEM[Rs+imm] 0 x 23 Rs Rt 16 -bit immediate lbu Rt, imm(Rs) Rt 1 MEM[Rs+imm] 0 x 24 Rs Rt 16 -bit immediate lhu Rt, imm(Rs) Rt 2 MEM[Rs+imm] 0 x 25 Rs Rt 16 -bit immediate sb Rt, imm(Rs) Rt 1 MEM[Rs+imm] 0 x 28 Rs Rt 16 -bit immediate sh Rt, imm(Rs) Rt 2 MEM[Rs+imm] 0 x 29 Rs Rt 16 -bit immediate sw Rt, imm(Rs) Rt 4 MEM[Rs+imm] 0 x 2 b Rs Rt 16 -bit immediate v Base / Displacement Addressing is used ² Memory Address = Rs (Base) + Immediate (displacement) ² If Rs is $zero then Address = Immediate (absolute) ² If Immediate is 0 then Address = Rs (register indirect) Control Flow and Arrays COE 301 – KFUPM slide 28
Next. . . v Control Flow: Branch and Jump Instructions v Translating If Statements and Boolean Expressions v Arrays v Load and Store Instructions v Translating Loops and Traversing Arrays v Addressing Modes Control Flow and Arrays COE 301 – KFUPM slide 29
Translating a WHILE Loop v Consider the following WHILE loop: i = 0; while (A[i] != value && i<n) i++; Where A is an array of integers (4 bytes per element) v Translate WHILE loop: $a 0 = &A, $a 1 = n, and $a 2 = value &A[i] = &A + i*4 = &A[i-1] + 4 li loop: lw beq addiu j done: . . . Control Flow and Arrays $t 0, $t 1, $t 0, $a 0, loop 0 0($a 0) $a 2, done $a 1, done $t 0, 1 $a 0, 4 # # # # COE 301 – KFUPM $t 0 = i = 0 $t 1 = A[i] (A[i] == value)? (i == n)? i++ $a 0 = &A[i] jump backwards to loop slide 30
Copying a String A string in C is an array of chars terminated with null char i = 0; do { ch = source[i]; target[i] = ch; i++; } while (ch != '