1 / 12

Ch. 6 Arrays

Ch. 6 Arrays. Arrays. Java: arrays are objects C++: arrays are primitive data For now: arrays as primitive data. int x[100]; char y[100]; int z[3]={4,-5,7}; // c++ char s[2]= {'h','i'}; // c++. x: .space 400 y: .space 100 z: .word 4, -5, 7 s: .byte 'h', 'i'.

orla-spence
Download Presentation

Ch. 6 Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch. 6 Arrays Comp Sci 251 - Arrays

  2. Arrays • Java: arrays are objects • C++: arrays are primitive data • For now: arrays as primitive data Comp Sci 251 - Arrays

  3. int x[100]; char y[100]; int z[3]={4,-5,7}; // c++ char s[2]= {'h','i'}; // c++ x: .space 400 y: .space 100 z: .word 4, -5, 7 s: .byte 'h', 'i' Array declaration and initialization Comp Sci 251 - Arrays

  4. char a[100]; a[5] = '*'; .text li $t0, '*' li $t1, 5 sb $t0, a($t1) .data a: .space 100 Array element access Index addressing Comp Sci 251 - Arrays

  5. Addressing modes Methods of specifying instruction operands • Immediate li $t0, 5 #operand is part of instruction • Register direct add $t0, $t0, $t1 #operand is in a register • Memory direct sw $t0, x #operand is in memory at address x Comp Sci 251 - Arrays

  6. Addressing modes • Indexed sw $t0, x($t1) #addr of operand is x + $t1 • Base register sw $t0, 4($t1) #addr of operand is 4 + $t1 • Register indirect sw $t0, ($t1) #addr of operand is in $t1 Comp Sci 251 - Arrays

  7. Examples of addressing modes: • Study • base_register.a in /shared/huen/251/ch06_array • indexed.a in /shared/huen/251/ch06_array • arraysP91.a in /shared/huen/251/ch06_array • length.a in /shared/huen/251/ch06_array Comp Sci 251 - Arrays

  8. char x[50]; int y[100]; int i; for(i=0; i<50; i++) x[i] = '*'; for(i=0; i<100; i++) y[i] = 0; .text .data x: .space 50 .align 2 y: .space 400 i: .space 4 Exercise: Initialize array elements Comp Sci 251 - Arrays

  9. Two-dimensional arrays • Problem: map two-dimensional structure to one-dimensional memory • Most compilers use "row-major" order Column index 0 1 Row index 0 2 3 4 0 1 2 3 4 0 0 1 1 2 1 2 3 4 0 1 2 2 3 4 Comp Sci 251 - Arrays

  10. Storage mapping function address of a[i , j ] = b + e(ir + j) e element size (bytes) b base address of array r row size = # columns Comp Sci 251 - Arrays

  11. Exercise: Translate to MIPS int x[100][50]; int n; x[n / 10, 2 * n + 1] = 7; Comp Sci 251 - Arrays

  12. Exercise: compute row sums int x[3][5]; int sum[3]; int i, j, total; for(i = 0; i < 3; i++){ total = 0; for(j = 0; j < 5; j++) total += x[i][j]; sum[i] = total; } Comp Sci 251 - Arrays

More Related