COMS 361 Computer Organization Title: Instructions Date: 9/23/2004 Lecture Number: 9
Announcements Homework 3 Due 9/28/04
Review Instructions MIPS load and store instruction format MIPS immediate instruction format Addressing MIPS branch instructions and format
Outline Instructions MIPS history SPIM unconditional branch J-Type instruction format MIPS history SPIM MIPS simulator
Control Flow MIPS also provides an unconditional branch instruction: j label Introduce a new type of instruction format j-type for branch instructions
Control Flow C++ code: if (i == j) { f = g + h; } else { f = g – h; } MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Endif Else: sub $s0, $s1, $s2 Endif:blah beq $s4, $s5, EQ sub $s3, $s4, $s5 j Endif EQ: add $s3,$s4,$s5 Endif:blah
Control Flow Unconditional branch instruction requires only an op code and an address to jump to Op code Address
Summary so far Instruction Meaning add $s1,$s2,$s3 $s1 = $s2 + $s3 sub $s1,$s2,$s3 $s1 = $s2 – $s3 lw $s1,100($s2) $s1=Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100]=$s1 bne $s4,$s5,L Next instr. is at L if $s4 != $s5 beq $s4,$s5,L Next instr. is at Label if $s4 == $s5 j Label Next instr. is at Label
Summary so far Instruction formats R op rs rt rd shamt funct I op rs rt 16 bit address J op 26 bit address
MIPS Background early 1980’s RISC: Reduced Instruction Set Computer Speed up the common, simple instructions Speed penalty for the uncommon instructions Large gain in performance CICS: Complex Instruction Set Computer Many complicated instructions Speed penalty for the simple instructions
MIPS John Hennessy at Stanford designed and fabricated a RISC called MIPS Play of the MIPS measure of performance Microprocessor without Interlocking Pipeline Stages
MIPS Memory Layout Memory cells consist of 8-bits (byte) Numbered (addressed) starting at 0 and increasing to the maximum allowable number Programs consist of Instructions Data Address Contents 0x00000000 0x00 0x00000001 0x0A 0x00000002 0x3E 0x00000003 0x10 0xFFFFFFFE 0xf0 0xFFFFFFFF 0x15 …
MIPS Memory Layout Memory must be organized Prevent data from being fetched as instructions Prevent instructions from being treated as data A programs address space is composed of three parts Text segment Contains program instructions Data segment Contains program data Stack segment Supports function calls
MIPS Memory Layout 0x7FFFFFFF Stack Segment … Data Segment Text Segment Reserved Stack starts at the top address space and grows down Data that instructions operate on 0x10000000 Instructions 0x00400000
MIPS Registers Register name Number Convention $zero $0 Constant 0 $at $1 Reserved for assembler $v0 - $v1 $2 - $3 Expression evaluation and function results $a0 - $a3 $4 - $7 Function arguments $t0 - $t7 $8 - $15 Temporary (not saved) $s0 - $s7 $16 - $23 Temporary (saved) $t8 - $t9 $24 - $25 $k0 - $k1 $26 - $27 Reserved for the OS kernel $gp $28 Global pointer (holds constant and global values) $sp $29 Stack pointer (last used location) $fp $30 Frame pointer (supports function calls) $ra $31 Return address for function calls
MIPS Registers 16 floating point registers name $f0 - $f15
SPIM A simulator for the MIPS R2000/R3000 processors Read and execute files containing assemble language Includes Debugger Interface Some operating system services Actually runs programs for MIPS computers How do we get a program into SPIM?
SPIM I/O The operating system kernel is the lowest level software running on a machine A program makes a system call to request the kernel to perform I/O operations The OS kernel implements I/O requests by communicating directly with the hardware An OS may have several hundred system calls SPIM provides 10 OS-like services Made through a syscall instruction Simulation of a simple OS
SPIM I/O SPIM programs request an I/O service Load the system call code into register $v0 Load arguments into registers $a0 - $a3 $f12 for floating point values
SPIM I/O Service Call code Arguments Result print_int 1 $a0 = integer print_float 2 $f12 = float print_double 3 $f12 = double print_string 4 $a0 = string read_int 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 $a0 = amount address in $v0 exit 10
Show me the code! SPIM Program MIPS/SPIM program files usually end in .s File is loaded into SPIM Assembled into MIPS machine code Executed and debugged in SPIM
SPIM Program ## exit.s - program simply makes a system call to exit ## ## text segment .text main: addi $v0, $zero, 10 # exit call code syscall # terminate program execution ## data segment ## end exit.s
SPIM Program ## exit.s - program simply makes a system call to exit ## text segment .text main: addi $v0, $zero, 10 # exit call code syscall # terminate program execution ## data segment ## end exit.s Comments in MIPS assembly code are denoted by the sharp (#) sign Similar to the C++ (//) comment. Starts at instance and goes to the end of the line Assembly code in NOT self-documenting. Use comments liberally, like associated with each instruction
SPIM Program ## exit.s - program simply makes a system call to exit ## text segment .text main: addi $v0, $zero, 10 # exit call code syscall # terminate program execution ## data segment ## end exit.s Comments indicating the start of the code After text segment is converted into its binary representation, it is stored in the text memory segment . indicates an assembler directive .text directs the assembler to treat what follows as instructions
Text Segment The source code format is relatively standard Proper indentation is fundamentally important in assembly language programming [] indicate optional fields Not all fields appear on a line of code [label:] operation [operand], [operand], [operand] [# comment]
SPIM Program ## exit.s - program simply makes a system call to exit ## text segment .text main: addi $v0, $zero, 10 # exit call code syscall # terminate program execution ## data segment ## end exit.s main: is a label Just as C++ programs need a main function, MIPS programs need a main label main: labels the first instruction of the program
Labels : Labels associate a symbol with Tells the assembler that the proceeding alphanumerics including (_) and (.) constitutes the label Opcodes are reserved words and are not permitted to be used as labels Use appropriate names for labels Labels associate a symbol with The address of an instruction The address of a variable
SPIM Program ## exit.s - program simply makes a system call to exit ## text segment .text main: addi $v0, $zero, 10 # exit call code syscall # terminate program execution ## data segment ## end exit.s Program is to simple call the OS with the exit call code In the real world the OS cleans up after the program The call code for exit is the decimal number 10, which needs to be put into the $v0 register (convention) addi with the proper operands can achieve this goal
SPIM Program ## exit.s - program simply makes a system call to exit ## text segment .text main: addi $v0, $zero, 10 # exit call code syscall # terminate program execution ## data segment ## end exit.s Call the OS with the proper call code in the $v0 register
Show me the program execute! SPIM Program Show me the program execute! And some things about SPIM City!! exit.s hello.s
MIPS Instructions MIPS instruction set architecture Assembly instructions that convert into machine (binary) instructions Assembly instructions can be converted directly into machine instructions One-to-one mapping Assembly instructions can be converted into more than one machine instruction One-to-many mapping Native machine instructions Pseudo-machine instructions Consists of more than one native machine instruction
MIPS Instructions Is that a real instruction or a Sears instruction?