Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly.

Similar presentations


Presentation on theme: "ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly."— Presentation transcript:

1 ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly Language Simulation Maciej Ciesielski www.ecs.umass.edu/ece/labs/vlsicad/ece232/spr2002/index_232.html

2 ECE 232 L7.Simul.2 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Outline °Review Sequence: compiler, assembler, linker, loader Procedure calls -Use of stack (pop, push) -Parameter passing, function return °MIPS Assembler Directives, system calls (I/O) Practical examples - read Appendix A: sections A1, A2 °MIPS Simulator Read Appendix A: section A9 °The following manual is put on reserve in library: MIPS Assembly Language Programming, by Robert Britton Read Sections 2 and 4

3 ECE 232 L7.Simul.3 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Running a program CompilerAssemblerLinker Loader C program Memory Assembly language program Machine language moduleLibrary routines object Machine language program executable

4 ECE 232 L7.Simul.4 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Memory Layout 0x00000000 0x00400000 0x10000000 0x70000000 0xFFFFFFFF Reserved Text Segment Data Segment Stack Segment Operating System

5 ECE 232 L7.Simul.5 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers R0$zero constant 0 R1$at reserved for assembler R2$v0 value registers & R3$v1 function results R4$a0 arguments R5$a1 R6$a2 R7$a3 R8$t0 temporary: caller saves... (callee can clobber) R15 $t7 MIPS: Software conventions for registers R16 $s0callee saves... (caller can clobber) R23 $s7 R24 $t8 temporary (cont’d) R25 $t9 R26 $k0 reserved for OS kernel R27 $k1 R28 $gp pointer to global area R29 $spStack pointer R30 $fpFrame pointer R31 $ra return Address

6 ECE 232 L7.Simul.6 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers MIPS Register File Register Naming Convention $0 : Constant Zero $v0, $v1 : Returned values from functions $a0 -$a3 : Arguments passed to functions $t0 - $t7 : Temporary registers (functions) $s0 -$s7 : Saved registers (main program) $sp : Stack Pointer $ra : Return address

7 ECE 232 L7.Simul.7 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Procedure call °Assume that procedure A computes f($a0,…,$a3) and uses temporary registers $s0 and $t0,$t. It is called by the main program. call procedure A procedure A.… jr $ra pc Memory pc + 4 1200 pc  $ra 2. Call procedure A: jal 1200 # $ra  pc+4; j 1200 1. Save registers $so, $t0, $t1 (push on stack) 3. Comute f($a0,..,$a3) and store result in $vo 4. Restore registers $so,$t0,$t1 (pop from stack) 5. Return from procedure: jr $ra # $pc  $ra

8 ECE 232 L7.Simul.8 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Example: Procedure call (not nested) °int leaf_example (int g, int h, int i, int j) {int f; f = (g + h) – (i + j); return f; } °Let parameter variables g, h, i, j, correspond to the argument registers $a0, $a1, $a2, $a3. Will use temp. registers $t0= (g + h) and $t1=(i + j). Function f will be stored in $s0. °Steps: °Save the old values of registers ($s0, $t0, $t1) on stack (push) °Issue a jal sub_address instruction ($ra  ret_addr, j sub_address) °Perform the computation for $t0, $t1, $s0 using argument registers °Save the value of f in a return value register $v0 °Restore the old values of the saved registers from stack (pop) °Finally, jump back to the calling routine, jr $ra (PC  return_address=PC+4)

9 ECE 232 L7.Simul.9 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Compiling a leaf procedure, cont’d Leaf_example:# label of the procedure Save the old values of registers ($s0, $t0, $t1) on stack (push) sub $sp, $sp, 12# adjust stack to make room for 3 items sw $t1, 8 ($sp)# save reg $t1 on stack …….# repeat for $t0, $s0 Perform the computation for $t0, $t1, $s0 using argument registers add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# $s0  (g + h) – (i + j) Copy the value of f into a return value register $v0 add $v0, $s0, $zero# returns f ($v0  $s0 + 0) Restore the old values of the saved registers from stack (pop ) lw $s0, 0 ($sp)# restore reg. $s0 for the caller ……. # repeat for $t0, $t1 … add $sp, $sp, 12# adjust the stack to delete 3 items Finally, jump back to the calling routine (PC  return address) jr $ra# PC  $ra

10 ECE 232 L7.Simul.10 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers SPIM Simulator

11 ECE 232 L7.Simul.11 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers SPIM Simulator °Console: this is where you enter input data and output is printed also, all system information is displayed (error handling)

12 ECE 232 L7.Simul.12 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Assembler syntax °Organize memory in segments: data, text °Directives tell assembler how to do it:.data# store in data segment items:.wordi1, i2, …# store integer words array:.space4096# reserve 1k space for arrray prompt:.asciiz str# place the string str in memory.globlmain# must be global.text# instructions go to text segment Main: your program goes here See examples (input data, compute, output data) Note: text in italics is your data: values, strings, names, labels, etc. Optional (if you need data)

13 ECE 232 L7.Simul.13 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Assembler directives °Help define and allocate space for data variables, arrays, and strings in the data segment of memory °Directs assembler to properly interpret data in memory.align 2# align data on a 4 byte (word) boundary.asciiz str# store the string str in memory.byte b1, …, bn# store the n values in successive memory bytes.word w1, …, wn# store the n words in successive memory words.data # store items in data segment (at address addr).globl symbol# label symbol is global (cannot be ref. by others)

14 ECE 232 L7.Simul.14 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Assembler directives - examples °C program:int Array[1024] MIPS assembly:.data# store subsequent items in data segment Array:.space 4096 # allocate 4x1024 bytes in Array °C program:int List[4] = {28, 13, 255, 2356} MIPS assembly:.data# can optionally specify base address List:.word 28, 13, 255, 2356# list of constants °To access third element (255) of List and place a copy in $s0: la$a0, list# a0  pointer to base of array List lw$s0, 12($a0)# s0  Mem[a0+12] in bytes Note: la = load address of base of array into register

15 ECE 232 L7.Simul.15 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers I/O Processing Example.data msg1:.asciiz "\n Please enter an integer: " msg2:.asciiz "\n Thank you ".globl main.text main: li$v0, 4# system call code for print_string la$a0, msg1# load address of msg1 into $a0 syscall# print the string li$v0, 5# system call code for read_string syscall# read the integer li$v0, 4# system call code for print_string la$a0, msg2# load address of msg2 into $a0 syscall# print the string

16 ECE 232 L7.Simul.16 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers Strings How are strings interpreted and stored? The following directive.asciiz "\n Please“ produces the following sequence of bytes in memory (in decimal).byte80, 108, 101, 97, 115, 101 P l e a s e See Appendix A2, and the ASCII Codes in text, Section 3.7,page 142

17 ECE 232 L7.Simul.17 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers System calls °SPIM provides a small set of operating system-like instructions (syscall instruction) °To request a service, a program loads: system call code into register $v0, and arguments into registers $a0 - $a3 for integers (or $f12 for floating point) °System call returns values put in $v0 (or $f0 for floating point) °Handles I/O: Allows to read in data and print out data °Handles errors, exceptions Prints error messages

18 ECE 232 L7.Simul.18 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers System services Service System call code ArgumentsResult print_integer1$a0 = integer print_float2$f12 = float print_double3$f12 = double print_string4$a0 = string read_integer5integer (in $v0) read_float6float (in $f0) read_double7double (in $f0) read_string8$a0=buffer $a1=length sbrk9$a0 = amountaddress (in $v0) exit 10

19 ECE 232 L7.Simul.19 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers A Complete Example sum:li$v0, 0 li$v1, 0# Initialize v0 and v1 to zero loop: blez$a1, retzz# If (a1  0) branch to return addi$a1, $a1, -1# Decrement loop count lw$t0, 0($a0)# Get a value from the array addi$a0, $a0, 4# Increment array pointer to next word bltz$t0, negg# If value is negative branch to negg add$v0, $v0, $t0# Add to the positive sum bloop# Branch around the next two instructions negg: add$v1, $v1, $t0# Add to the negative sum bloop# Branch to loop retzz:jr$ra# Return Given a list of integers, compute and print sum of positive numbers sum of negative numbers Subroutine sum (to be placed at the end of program):

20 ECE 232 L7.Simul.20 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers A Complete Program Example, cont’d.data array:.word -4, 5, 8, -1 msg1:.asciiz "\n The sum of the positive values = " msg2:.asciiz "\n The sum of the negative values = ".globl main.text main: li$v0, 4# system call code for print_str la$a0, msg1# load address of msg1. into $a0 syscall# print the string la$a0, array# Initialize address Parameter li$a1, 4# Initialize length Parameter jalsum# Call sum move$a0, $v0# move value to be printed to $a0 li$v0, 1# system call code for print_int syscall# print sum of positive values li$v0, 4# system call code for print_str la$a0, msg2# load address of msg2. into $a0 syscall# print the string li$v0, 1# system call code for print_int move$a0, $v1# move value to be printed to $a0 syscall# print sum of negative values li$v0, 10# terminate program run and syscall# return control to system


Download ppt "ECE 232 L7.Simul.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 7 MIPS Assembly."

Similar presentations


Ads by Google