Introduction to Lab 1 Jos Nelson Amaral General
Introduction to Lab #1 José Nelson Amaral
General Intro to 229 Labs • In 229, a “lab” is a programming assignment: • A lab requires many more hours of work than the time allocated for lab sessions. • Lab sessions are “consulting hours” when TAs are available to answer questions and to help. • Reading/work prior to the lab date/time is essential. • The lab assignments will be progressively more difficult, and will require more time as the term advances. • A CMPUT 229 lab is not a “lab” in the sense of a chemistry lab.
Part #1 • Read Appendix titled "Assemblers, Linkers, and the SPIM Simulator" (specially section 9): – In the 4 th edition of the book, this is appendix B – In the 5 th edition of the book, this is appendix A • Read the SPIM Tutorial (available online): – http: //pages. cs. wisc. edu/~larus/HP_App. A. pdf
Part #2 • Self-guided tutorial-style introduction to usage of XSPIM. Part #3 • Simple exercise to illustrate use of SPIM.
Part #4 • Understand data storage in memory. • Question #6: Understand little/big endianess and conversion to/from ASCII. • Question #7: Understand 2 -complement integers • Question #8: Assembly directives
Part #5: Find bugs in lab 1 -broken. s The program lab 1 -broken. s was written to check whether a string is a palindrome. It should check if “racecar” Is the same as “racecar” But lab 1 -broken. s is not working as it should. Your job is to read and understand the program and to report what are the errors in this program.
Part #5 submission • You will describe the bugs in a text file called bugs. txt and submit this file. • The solution for parts 1 -5 are answers to the questions in the lab assignment. – There is no specified format for these answers. Just use a reasonable formatting and provide clear and concise answers.
System call table Chapter 2 — Instructions: Language of the Computer — 8 P-H pp. A-44
Part #6: The calculator Program Write a MIPS assembly program that acts as a calculator for reverse Polish notation/postfix expressions. Infix notation: (1 + 2) * 3 = 9 Postfix notation: 12+3* =9
Types of input tokens for calculator Token Type Value OPERAND non-negative integer PLUS -1 MINUS -2 TERMINATION -3
Operation of the calculator • read a token from the input list • if token == OPERAND (a non-negative value) – push value into the stack • if token == PLUS or token == MINUS – pop two topmost values from the stack – perform operation – push result into the stack • if token == TERMINATION – print out the value that is on top of the stack – terminate the program
How does the stack grow? Stack: Top. Of. The. Stack 0 x 10001024 0 x 10001020 0 x 1000101 C 0 x 10001018 5 1 3 4
Input Token List: 0 x 1000100 C 0 x 10001008 0 x 10001004 0 x 1000 -3 -2 1 5 Stack: 0 x 10001024 0 x 10001020 0 x 1000101 C 0 x 10001018 4 Token Type: TERMINATION OPERAND MINUS 4 5 1
Formatting and Style • Check the lab grading marksheet
Assembler Syntax comments begin with a sharp sign (#) and run to the end of the line. identifiers are alphanumeric sequences, underbars (_), and dots (. ) that do not begin with a number. labels are identifiers placed at the beginning of a line, and followed by a colon. item: main: lw Loop: add add lw bne add Chapter 2 — Instructions: j Language of the Computer — Exit: 15 . data. word 1. text. globl main $s 3, item $t 1, $s 3 # $t 1 ← 2 * i $t 1, $t 1 # $t 1 ← 4 * i $t 1, $s 6 # $t 1 ← Addr(save[i]) $t 0, 0($t 1) # $t 0 ← MEM[save[i]] $t 0, $s 5, Exit # if save[I] ≠ k goto Exit $s 3, $s 4 # i←i+j Loop # goto Loop P-H. p. B-10
Assembler Directives. data identifies the beginning of the data segment (in this example this segment contains a single word). . word 1 stores the decimal number 1 in 32 -bits (4 bytes) . text identifies the beginning of the text segment (where the instructions of the program are stored). . globl main item: main: lw Loop: add add lw bne add Chapter 2 — Instructions: j Language of the Computer — Exit: 16 declares the label main global (so that it can be accessed from other files). . data. word 1. text. globl main $s 3, item $t 1, $s 3 # $t 1 ← 2 * i $t 1, $t 1 # $t 1 ← 4 * i $t 1, $s 6 # $t 1 ← Addr(save[i]) $t 0, 0($t 1) # $t 0 ← MEM[save[i]] $t 0, $s 5, Exit # if save[I] ≠ k goto Exit $s 3, $s 4 # i←i+j Loop # goto Loop
File lab 1 -p 1. s pseudo instruction that loads the immediate value in the register # What's going on here ? . text main: li $a 1, 5 la $t 0, val xor $t 1, $t 1 xor $t 2, $t 2 loop: sub $t 3, $a 1, $t 2 blez $t 3, exit lw $t 4, 0($t 0) add $t 1, $t 4 add $t 2, 1 addu $t 0, 4 j loop pseudo instruction that loads the address of specified label into register Chapter 2 — Instructions: Language of the Computer — 17 exit: div $t 5, $t 1, $a 1 li $v 0, 4 la $a 0, output. Msg syscall li $v 0, 1 add $a 0, $t 5 syscall li $v 0, 4 la $a 0, newln syscall jr $ra. data val: . word 12, 34, 56, 78, 90 output. Msg: . asciiz "n Result = " newln: . asciiz "nn" OS-style call to obtain services from SPIM: $a 0 -$a 3: arguments $v 0: system call code before the call; return value after the call. (see page A-43).
Files to Submit • There are three files to submit: – lab 1. txt – lab 1. s – bugs. txt – Make sure lab 1. s does not contain a main label. Your code must go under the label ‘calculator’.
- Slides: 18