Conditional Move and Arrays COE 301 Computer Organization











![Initializing a Column of a Matrix M = new int[10][5]; // allocate M on Initializing a Column of a Matrix M = new int[10][5]; // allocate M on](https://slidetodoc.com/presentation_image_h/ac94c40b35de6e6752cbcbf05d6a3ebd/image-12.jpg)
- Slides: 12
Conditional Move and Arrays COE 301 Computer Organization ICS 233 Computer Architecture and Assembly Language Dr. Marwan Abu-Amara College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals [Adapted from slides of Dr. M. Mudawar, KFUPM]
Presentation Outline v Conditional Move Instructions v Arrays Control Flow and Arrays COE 301 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 2
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 $t 0, $0, L 1 addu $t 1, $t 2, $t 3 j L 2 L 1: subu $t 1, $t 2, $t 3 L 2: . . . 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 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 3
Next. . . v Conditional Move Instructions v Arrays Control Flow and Arrays COE 301 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 4
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 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 5
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 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 6
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 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 7
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 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 8
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 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 9
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 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 10
Element Addresses in a 2 D Array Address calculation is essential when programming in assembly COLS 0 1 … j … COLS-1 0 1 ROWS … i … ROWS-1 &matrix[i][j] = &matrix + (i×COLS + j) × Element_size Control Flow and Arrays COE 301 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 11
Initializing a Column of a Matrix M = new int[10][5]; // allocate M on the heap int i; for (i=0; i<10; i++) { M[i][3] = i; } # &M[i][3] = &M + (i*5 + 3) * 4 = &M + i*20 + 12 li $a 0, 200 # $a 0 = 10*5*4 = 200 bytes li $v 0, 9 # system call 9 syscall # allocate 200 bytes move $t 0, $v 0 # $t 0 = &M li $t 1, 0 # $t 1 = i = 0 li $t 2, 10 # $t 2 = 10 L: sw $t 1, 12($t 0) # store M[i][3] = i addiu $t 1, 1 # i++ addiu $t 0, 20 # $t 0 = &M[i][3] bne $t 1, $t 2, L # if (i != 10) loop back Control Flow and Arrays COE 301 / ICS 233 – Computer Organization – KFUPM © Muhamed Mudawar – slide 12












