f14_midterm_review_solutions.pdf

If the remainder is 0 we have a composite add 2 2 11

Info icon This preview shows pages 3–6. Sign up to view the full content.

; if the remainder is 0, we have a composite add $2, $2, $11 ; add 1 to the divisor beq $0, $0, loop comp: add $3, $0, $0 end: ; restore registers lis $1 .word 20 add $30, $30, $1 lw $1, -4($30) lw $2, -8($30) lw $4, -12($30) lw $5, -16($30) lw $11, -20($30) jr $31 3
Image of page 3

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

4. When a number is stored in a register, how can MIPS tell if that number is a binary number, a signed int, an unsigned int, or a hexadecimal? It doesn’t. A sequence of bits is only ever a sequence of bits - it is up to the instruction reading it to interpret it however it needs to. For example, the value “0x80000000” is considered positive for an instruction like sltu , but negative for slt . 3 Writing an Assembler 1. Find all the errors in the following MIPS assembly language program. start: lis 4 (should be "$4") .word $0x1 (should be "0x01") start: (defined label "start" twice) sw $1, $30, 4 (wrong syntax for sw; should be sw $1, 4($30)) beq $0, $1(4) (wrong syntax for beq; should be beq $0, $1, 4) sub $30, $30, 4 middle: (can’t define a label after an instruction) ; add $2, $2, $2 end: beq $4, $5, run (label "run" not defined anywhere) slt $9 $8 $7 (missing commas in between registers) 2. Create the symbol table for the following MIPS assembly program. constants: X lis $11 X .word 1 add: X add $1, $1, $11 X add $3, $11, $11 doneadd: push: X sw $3, (-4)$30 X lis $4 X .word 4 X sub $30, $30, $4 end: X jr $31 postend: (Non-null lines marked with X) constants: 0x0 add: 0x8 doneadd: 0x10 push: 0x10 end: 0x20 postend: 0x24 To fill out this symbol table, keep in mind that the address of an isntruction is the number of non-null lines before it, multiplied by 4 (an instruction is 4 bytes). 4
Image of page 4
3. Assemble the MIPS instruction sw $4, 8($10) . First, express ‘t,’ ‘s,’ and ‘i’ in binary, using 5 bits for s and t, and 16 bits for i: s = 01010, t = 00100, i = 0000 0000 0000 1000 Next, copy over the binary encoding of sw: sw $t, i($s) => 1010 11ss ssst tttt iiii iiii iiii iiii Fill in the five ‘s’ digits with the binary encoding of ‘s” sw $t, i($s) => 1010 1101 010t tttt iiii iiii iiii iiii Repeat the process for ‘t’: sw $t, i($s) => 1010 1101 0100 0100 iiii iiii iiii iiii And for i: sw $t, i($s) => 1010 1101 0100 0100 0000 0000 0000 1000 4 Relocating and Linking 1. Why do programs need to be relocatable? In order for a MIPS program to be relocatable, what instructions need to be changed? Programs need to be relocatable so that the loader can place them into any RAM location and have them execute normally, without relying on starting from specific memory addresses. This lets one machine run potentially many programs simultaneously. The only instructions that need to be changed are .word instructions that take a label operand. Because branches take a relative offset (i.e. branch ahead/back X instructions), the entire program can be moved without affecting these instructions. .word
Image of page 5

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

Image of page 6
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '09
  • TroyVasiga
  • Binary numeral system, Positional notation, Decimal, Regular expression, Nondeterministic finite state machine

{[ 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