2-12-09 - Run time environment of a MIPS program Stack...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Run time environment of a MIPS program Stack pointer Low address Temporary local variables Return address Saved argument registers beyond a0-a3 Growth of stack Frame pointer High address A translation hierarchy HLL program COMPILER Assembly language program ASSEMBLER Machine language module LINKER Library routine Executable machine language program LOADER Memory What are Assembler directives? Instructions that are not executed, but they tell the assembler about how to interpret something. Here are some examples: . text {Program instructions here} . data {Data begins here} . byte 84, 104, 101 . asciiz “The quick brown fox” . float f1,. . . , fn . word w1, . . . . wn How does an assembler work? In a two-pass assembler PASS 1: Symbol table generation PASS 2: Code generation To be explained in the class … Other architectures Not all processors are like MIPS. Example. Accumulator-based machines A single register, called the accumulator, stores the operand before the operation, and stores the result after the operation. Load Add Store x y z # into acc from memory # add y from memory to the acc # store acc to memory as z Can we have an instruction add z, x, y # z:= x + y, (x, y, z in memory) ? For some machines, YES, not in MIPS Load-store machines MIPS is a load-store architecture. Only load and store instructions access the memory, all other instructions use registers as operands. What is the motivation? Register access is much faster than memory access, so the program will run faster. Reduced Instruction Set Computers (RISC) • The instruction set has only a small number of frequently used instructions. This lowers processor cost, without much impact on performance. • All instructions have the same length. • Load-store architecture. Non-RISC machines are called CISC (Complex Instruction Set Computer). Example: Pentium Another classification 3-address 2-address 1-address add r1, r2, r3 add r1, r2 add r1 (r1 ¨ r2 + r3) (r1 ¨ r1 + r2) (to the accumulator) (see below) 0-address or stack machines Example of stack architecture Push x Push y Push z Add Multiply Pop z x x y z x y+z x * (y+z) Computes z = x * (y + z) Computer Arithmetic How to represent negative integers? The most widely used convention is 2’s complement representation. +14 = -14 = 0, 1 1 1 0 1, 0 0 1 0 Largest integer represented using n-bits is Smallest integer represented using n-bits is +2 -2 n-1 -1 n-1 Review binary-to decimal and binary-to-hex conversions. Review BCD (Binary Coded Decimal) and ASCII codes. How to represent fractions? Overflow +12 = +2 add +14 = = 0, 1 1 0 0 0, 0 0 1 0 +12 = +7 = 0, 1 1 0 0 0, 0 1 1 1 add 0, 1 1 1 0 ? = 1, 0 0 1 1 Addition of a positive and a negative number does not lead to overflow. How to detect overflow? Exceptions MIPS coprocessor has a cause register that contains a 4bit code to identify the cause of an exception Cause register pending interrupt exception code 15-10 5-2 MIPS instructions that cause overflow (or some other violation) lead to an exception (also called an interrupt), and transfer control to a predefined address to invoke a routine (exception handler) for handling the exception. L: add $t0, $t1, $t2 Return address (L+4) is saved in EPC Next instruction overflow! Exception handler routine ra ¨ EPC jr ra Exceptions cause unscheduled procedure calls. The following sequence of MIPS instructions can detect overflow in signed addition of $t1 and $t2: addu $t0, $t1, $t2 xor $t3, $t1, $t2 slt $t3, $t3, $zero # add unsigned # check if signs differ # $t3=1 if signs differ bne $t3 $zero, no_overflow xor $t3, $t0, $t1 slt $t3, $t3, $zero # sum sign = operand sign? # if not, then $t3=1 bne $t3, $zero, overflow no_overflow: ... ... overflow: <Do something to handle overflow> More Programming Examples Copying a string a0 Each char is represented by an ASCII byte. The string is terminated by a Null in ASCII). Reg s0 will hold the array index. array y a1 s0 . array x I O W A null add $s0, $zero, $zero L1: add $t1, $a1, $s0 Load byte lb $t2, 0($t1) add $t3, $a0, $s0 sb $t2, 0($t3) addi $s0, $s0, 1 bne $t2,$zero, L1 #i=0 # address of y[i] in t1 # t2 = y[i] # address of x[i] in t3 # x[i] = y[i] # i = i+1 # if y[i]≠0 then goto L1 ...
View Full Document

Ask a homework question - tutors are online