Download Lecture 7

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
ECE 232 L7.Simul.1
Adapted from Patterson 97 ©UCB
Copyright 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
ECE 232 L7.Simul.2
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
Running a program
C program
Compiler
Assembly language program
Assembler
object
Machine language module
Library routines
Memory
Linker
executable
Machine language program
Loader
ECE 232 L7.Simul.3
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
Memory Layout
0x00000000
Reserved
0x00400000
Text Segment
0x10000000
Data Segment
Stack Segment
0x70000000
Operating System
0xFFFFFFFF
ECE 232 L7.Simul.4
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
MIPS: Software conventions for registers
R0 $zero constant 0
R16 $s0
R1 $at
reserved for assembler
...
R2 $v0
value registers &
R23 $s7
R3 $v1
R4 $a0
function results
arguments
callee saves
(caller can clobber)
R24 $t8 temporary (cont’d)
R25 $t9
R5 $a1
R26 $k0 reserved for OS kernel
R6 $a2
R27 $k1
R7 $a3
R28 $gp pointer to global area
R8 $t0
temporary: caller saves
R29 $sp
Stack pointer
...
(callee can clobber)
R30 $fp
Frame pointer
R15 $t7
ECE 232 L7.Simul.5
R31 $ra
Adapted from Patterson 97 ©UCB
return Address
Copyright 1998 Morgan Kaufmann Publishers
MIPS Register File
Register Naming Convention
$0 :
$v0, $v1 :
$a0 -$a3 :
$t0 - $t7 :
$s0 -$s7 :
$sp :
$ra :
ECE 232 L7.Simul.6
Constant Zero
Returned values from functions
Arguments passed to functions
Temporary registers (functions)
Saved registers (main program)
Stack Pointer
Return address
Adapted from Patterson 97 ©UCB
Copyright 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.
Memory
pc
pc + 4
1200
pc  $ra
ECE 232 L7.Simul.7
call procedure A
procedure A
.…
jr $ra
Adapted from Patterson 97 ©UCB
1. Save registers $so, $t0, $t1
(push on stack)
2. Call procedure A:
jal 1200 # $ra  pc+4; j 1200
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
Copyright 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)
ECE 232 L7.Simul.8
Adapted from Patterson 97 ©UCB
Copyright 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
sw $t1, 8 ($sp)
…….
# adjust stack to make room for 3 items
# save reg $t1 on stack
# repeat for $t0, $s0
Perform the computation for $t0, $t1, $s0 using argument registers
add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1
# $t0  g + h
# $t1  i + j
# $s0  (g + h) – (i + j)
Copy the value of f into a return value register $v0
# 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
add $v0, $s0, $zero
Finally, jump back to the calling routine (PC  return address)
jr $ra
ECE 232 L7.Simul.9
# PC  $ra
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
SPIM Simulator
ECE 232 L7.Simul.10
Adapted from Patterson 97 ©UCB
Copyright 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)
ECE 232 L7.Simul.11
Adapted from Patterson 97 ©UCB
Copyright 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:
array:
.word i1, i2, …
.space 4096
# store integer words
# reserve 1k space for arrray
prompt:
.asciiz str
# place the string str in memory
.globl
# must be global
Optional
(if you need data)
main
.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.
ECE 232 L7.Simul.12
Adapted from Patterson 97 ©UCB
Copyright 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 <addr>
# store items in data segment (at address addr)
.globl symbol
# label symbol is global (cannot be ref. by others)
ECE 232 L7.Simul.13
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
Assembler directives - examples
° C program:
int Array[1024]
MIPS assembly:
.data
Array: .space 4096
° C program:
# store subsequent items in data segment
# allocate 4x1024 bytes in Array
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
ECE 232 L7.Simul.14
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
I/O Processing Example
.data
.asciiz
.asciiz
.globl
.text
msg1:
msg2:
"\n Please enter an integer: "
"\n Thank you "
main
main:
ECE 232 L7.Simul.15
li
la
syscall
$v0, 4
$a0, msg1
# system call code for print_string
# load address of msg1 into $a0
# print the string
li
syscall
$v0, 5
# system call code for read_string
# read the integer
li
la
syscall
$v0, 4
$a0, msg2
# system call code for print_string
# load address of msg2 into $a0
# print the string
Adapted from Patterson 97 ©UCB
Copyright 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)
.byte
80, 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
ECE 232 L7.Simul.16
Adapted from Patterson 97 ©UCB
Copyright 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
ECE 232 L7.Simul.17
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
System services
Service
System call code
Arguments
Result
print_integer
1
$a0 = integer
print_float
2
$f12 = float
print_double
3
$f12 = double
print_string
4
$a0 = string
read_integer
5
integer (in $v0)
read_float
6
float (in $f0)
read_double
7
double (in $f0)
read_string
8
$a0=buffer
$a1=length
sbrk
9
exit
10
ECE 232 L7.Simul.18
$a0 = amount
Adapted from Patterson 97 ©UCB
address (in $v0)
Copyright 1998 Morgan Kaufmann Publishers
A Complete Example
• 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):
sum:
li
li
$v0, 0
$v1, 0
# Initialize v0 and v1 to zero
blez
addi
lw
addi
bltz
add
b
$a1, retzz
$a1, $a1, -1
$t0, 0($a0)
$a0, $a0, 4
$t0, negg
$v0, $v0, $t0
loop
# If (a1  0) branch to return
# Decrement loop count
# Get a value from the array
# Increment array pointer to next word
# If value is negative branch to negg
# Add to the positive sum
# Branch around the next two instructions
add
b
jr
$v1, $v1, $t0
loop
$ra
# Add to the negative sum
# Branch to loop
# Return
loop:
negg:
retzz:
ECE 232 L7.Simul.19
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers
A Complete Program Example, cont’d
.data
.word
.asciiz
.asciiz
.globl
.text
array:
msg1:
msg2:
main:
ECE 232 L7.Simul.20
-4, 5, 8, -1
"\n The sum of the positive values = "
"\n The sum of the negative values = "
main
li
la
syscall
la
li
$v0, 4
$a0, msg1
$a0, array
$a1, 4
# system call code for print_str
# load address of msg1. into $a0
# print the string
# Initialize address Parameter
# Initialize length Parameter
jal
sum
# Call sum
move
li
syscall
li
la
syscall
li
move
syscall
li
syscall
$a0, $v0
$v0, 1
# move value to be printed to $a0
# system call code for print_int
# print sum of positive values
# system call code for print_str
# load address of msg2. into $a0
# print the string
# system call code for print_int
# move value to be printed to $a0
# print sum of negative values
# terminate program run and
# return control to system
$v0, 4
$a0, msg2
$v0, 1
$a0, $v1
$v0, 10
Adapted from Patterson 97 ©UCB
Copyright 1998 Morgan Kaufmann Publishers