You are on page 1of 5

CS 241 Tutorial 2

Stephen Whitmore Week 4, 2008

Tutorial 2 Topics Review: What is a symbol table? Problem 1: Create (by hand) a symbol table for these MIPS programs. Problem 2: Assemble this (short) MIPS program that uses labels.

What is a Symbol Table?

A symbol table is a data structure that stores the name and location of every label in a MIPS program. Important things to remember are: Locations are stored in bytes. There are four bytes per each word/instruction. Your assembler will need to handle MIPS programs with 10,000+ labels, meaning a data structure with fast storing and look-up is essential. Recommended are the Java HashMap or the Scheme hash-table, which oer approximately O(1) operations. The equation for determining which line a label in a beq or bne instruction is: i(L+4) , where L is the current instructions location, and i is the labels 4 location. 3 Problem 1a

Given the following MIPS assembly code, create a symbol table containing all of the labels used. ; Assume program is to be run with mips.twoints ; $1 - first integer ; $2 - second integer
1

lis $3 .word 100 bound: lis $4 .word -100 lis $1 .word bound slt $5, $1, $3 slt $6, $2, $4 beq $5, $0, move bne $6, $0, jump bne $5, $0, leap leap: beq $6, $0, bound shuffle: ; store leaps location in $7

lis $7 .word leap jump: move: jr $31 4 Problem 1a: Solution * 4 = 8 * 4 = 44 12 * 4 = 48 * 4 = 52 * 4 = 52


2

bound: 2 leap: 11 shuffle: jump: 13 move: 13

Problem 1b

Given the following MIPS assembly code, create a symbol table containing all of the labels used. ; Assume program is to be run with mips.twoints ; $1 - first integer ; $2 - second integer lis $3 .word 0xFF123 start: next: add $1, $1, $3 sub $3, $3, $1 sw $3, -4($30) lis $4 .word 4 sub $30, $30, $4 lis $5 .word -1 beq $0, $0, skip handleNeg: ; handle negativity mult $3, $5 mflo $3 skip:

; Check if $3 is negative slt $6, $3, $0 bne $6, $0, handleNeg loop:
3

add $3, $3, $5 bne $3, $0, loop end: endplus1: jr $31 6 Problem 1a: Solution

start: 2 * 4 = 8 next: 2 * 4 = 8 handleNeg: 11 * 4 = 44 skip: 13 * 4 = 52 loop: 15 * 4 = 60 end: 17 * 4 = 68 endplus1: 17 * 4 = 68 7 Problem 2

Assemble the following (short) MIPS assembly code into binary, taking into account what values should be substituted in place of the labels. ; Problem 2: Assemble this (short!) MIPS program into binary format. myLittleLabelThatIsOhSoVeryLong: lis $28 .word whyAreAllOfTheseLabelsSoPainfullyLong

bne $0, $0, pleaseEndThis whyAreAllOfTheseLabelsSoPainfullyLong: beq $0, $0, myLittleLabelThatIsOhSoVeryLong pleaseEndThis: ; End program

jr $31 8 Problem 1a: Solution ; ; ; ; ; lis $28 .word whyAreAllOfTheseLabelsSoPainfullyLong bne $0, $0, pleaseEndThis beq $0, $0, myLittleLabelThatIsOhSoVeryLong jr $31

0x0000E014 0x0000000C 0x14000001 0x1000FFFC 0x03E00008

You might also like