{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ExampleSoln1 - Review Problem 0 As you wait for class to...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Review Problem 0 As you wait for class to start, answer the following question: What is important in a computer? What features do you look for when buying one? OS MHz Pentium vs. Athlon Main memory (DRAM) C h (SRAM) Caches (SRAM) Peripherals 59 Slide 0: No Background required!
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Review Problem 1 Programming languages have many instructions, but they fall under a few basic types. One is arithmetic (+, -, *, /, etc). What are the others? Logical: and, or, etc. C t l fl if th l t d ll Control flow: if-then-else, goto, procedure call Iteration: for, while, do-until Librarys: file I/O, graphics Storage: new, malloc, class methods, arrays, structs Probably others Probably others… 60 Slide 9: Assembly Language
Image of page 2
Review Problem 1a In assembly, copy the value of register $a0 to register $t0 add $t0, $a0, $0 61 Slide 13: Registers
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Review Problem 2 In assembly, compute the average of $a0, $a1, $a2, $a3, and put into $v0 add $v0, $a0, $a1 add $v0, $v0, $a2 add $v0, $v0, $a3 # v0 = sum(a0,a1,a2,a3) sra $v0, $v0, 2# v0 = sum(a0,a1,a2,a3)/4 62 Slide 14: Basic Operations
Image of page 4
Review Problem 3 What would the results of this C++ code be in memory? Assume we start using memory at 1000. 1000 1001 1002 1003 1016 1017 1018 1019 0 0 3 236 ‘y’ obj: obj->b[0] . . Struct { char *a, *b; } foo; 1004 1005 1006 1020 1021 1022 0 0 3 obj->a . . . . obj >b[7] Struct { char a, b; } foo; foo *obj; obj = new foo; obj->a = new char[4]; obj->b = new char[8]; 1007 1008 1009 1010 1023 1024 1025 1026 244 0 0 3 obj->b obj->b[7] obj->a[1] = ‘x’; obj->b[2] = ‘y’; 1011 1012 1014 1013 1027 1028 1030 1029 248 ‘x’ obj->a[0]: . 63 1015 1031 Slide 18: Data Storage . obj->a[3]:
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Review Problem 4 In assembly, replace the value in $a0 with its absolute value. (bgtz) bgez $a0, WAS_POSITIVE sub $a0, $zero, $a0 WAS_POSITIVE: 64 Slide 23: Control Flow
Image of page 6
Review Problem 5 Register $a0 has the address of a 3 integer array. Set $v0 to 1 if the array is sorted (smallest to largest), 0 otherwise. Lw $t0 0($a0) // array[0] Lw $t0, 0($a0) Lw $t1, 4($a0) // array[1] Lw $t2, 8($a0) // array[2] Add $v0, $0, $0 // set to 0 initially // Slt $t4, $t1, $t2 // is [0] < [1]? Beq $t4, $0, UNSORTED Slt $t4, $t2, $t3 // is [1] < [2]? Beq $t4, $0, UNSORTED Addi $v0, $0, 1 // set to 1 – sorted! UNSORTED: 65 Slide 25: Comparison Operators
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Review Problem 6 For the following code, can you ever have labels A1 == A2? A1 == A3? A1 == A4? lw $t0, 100($zero) beq $t0, $0, A2 A1: lbu $t5, 0($t0) A4: beq $t5, $zero, A4 slt $t6, $t5, $t2 bne $t6, $zero, A3 A2: A2 = 4. If lbu at 4<<2 (addr 16) then A1 == A2. Start code at 8 slt $t6, $t3, $t5 A3: add $t5, $t5, $t4 sb $t5, 0($t0) addi $t0 $t0 1 A3 = 1, smallest A1 can be is 2, can’t do it $t0, $t0, 1 j A1 can t do it A4 = -1, which is 16x1’s, or 2^16-1. Can do Start code at (2^16 1)*4 8 66 Slide 29: Labels Example Can do. Start code at (2^16-1)*4-8
Image of page 8
Review Problem 7 Sometimes it can be useful to have a program loop infinitely. We can do that, regardless of location, by the instruction: LOOP: BEQ $7, $7, LOOP Convert this instruction to machine code 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 000100 00111 00111 1111111111111111 OP RS RT 16 bit Address/Immediate BEQ Opcode is 04 RS and RT are $7 Branch goes to PC + 4 + Offset*4. To stay the same, offset=-1 1 = all ones 67 Slide 33: I-Type -1 = all ones
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Review Problem 8 We goofed, and wrote code with: add $t0, $t1, $t4, when we meant to write sub $t0, $t1, $t4. The instruction is at location Mem[0]. What’s the simplest program to fix this?
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern