Lec16

Post on 14-Dec-2015

213 views 0 download

description

3453

Transcript of Lec16

MIPS - Course Assembly Language• MIPS

– “Microprocessor without Interlocked Pipeline Stages”– ISA used primarily in embedded systems

• PlayStation 1 & 2, Nintendo64, & PSP• digital cameras, DVD players, routers, wireless phones, HDTVs, etc.

• Why MIPS?– has a long educational legacy– good textbook– still widely used

• MIPS Architecture and Assembly Language Overview

MIPS• Designed in early 1980s by John Hennessy

– our textbook author and Stony Brook CS alumni

• Is a Reduced Instruction Set Computer architecture (RISC)– as opposed to Complex Instruction Set Computer

architecture (CISC)• once used by most PCs• starting with the 486 and Pentium-I processors Intel

implemented a RISC core in its processors

MARS – Course IDE• MARS

– MIPS Assembler & Runtime Simulator– freely downloadable 2.1 MB JAR file– http://courses.missouristate.edu/KenVollmar/MARS/

– Online tutorial:• http://courses.missouristate.edu/KenVollmar/MARS/tutorial.htm

• SPIM– http://sourceforge.net/projects/spimsimulator/files/

• All MIPS programming assignments should be done using MARS & or SPIM depending on the assignment specification

Why don’t CPUs just use C?

What’s so good about abstraction?• CPU makers can make CPUs better

– faster– more efficient– easier to program

• Same for high level language programmers– you write a program in C– a compiler translates the C code into MIPS– an assembler translates the MIPS code into bytecode

hello.c/* hello.c* Purpose - print hello*/

#include <stdio.h>

int main(void){

printf("Hello, world!\n");return 0;

}

hello.asm## hello.asm - print out "hello world"

# text Segment #.text # tells assembler program code starts here.globl main # defines label for execution startmain: # execution starts herela $a0,str # put string address into a0li $v0,4 # system call to printsyscall # out a stringli $v0, 10 # Load exit syscall valuesyscall # Exit

# data segment #.data # tells assembler data segment begins herestr: .asciiz "hello world\n" # declaration of a string

bytecode executed by hardware001111000000000100010000000000010011010000100100000000000000000000100000000000100000000000000100000000000000000000000000000011000010000000000010000000000000101000000000000000000000000000001100

A closer look at hello.asm## hello.asm - print out "hello world"

# text segment #.text # tells assembler program code starts here.globl main # makes main available externallymain: # execution starts herela $a0,str # put string address into a0li $v0,4 # system call to printsyscall # out a stringli $v0,10 # Load exit syscall valuesyscall # Exit

# data segment #.data # tells assembler data segment begins herestr: .asciiz "hello world\n" # declaration of a string

Let’s read the comments

Does main start the program?• Not necessarily• If we run the program, it simply starts at the top

# hello.asm - print out "hello world"

# text segment #.text # tells assembler instructions starts here.globl main # makes main available externallymain: # execution starts here…

Comments• Start with a #

• Similar to // in Java

• Everything after # on a line is a comment

• Ex:

la $a0,str # put string address into a0

What else did we find?• Lines that start with ‘.’ are assembler directives

– Ref: A-46 – A-48– .text : everything below this is source code– .globl main : makes main available externally– .data : specifies the area where global variables

are to be declared– .asciiz : for declaring a null terminated string

And what else?• Lines starting with region_name: specify labels for

memory addresses of instructions– for methods, global variables, or blocks of code

• Ex:main :…str :

• Everything below “main :” is inside the main method until another region of code is reached

What’s really happening with main?• When the assembler finds lines starting with region_name: , it records the name and the memory address of the instruction in a table

• Why?– because other pieces of code might refer to it– they won’t want to use the memory address– they will want to use the textual name– during assembly, memory address (a 32-bit number)

replaces its textual name

Let’s make an empty.asm template.text.globl main

main:# put your instructions here

.data# put your global variables here

And now for the instructions• MIPS has a number of instructions to use• Instructions exist for fairly simple purposes:

– Data Transfer– Arithmetic– Logical– Conditional Branch– Unconditional Branch– System Call

Data Transfer Instructions• For moving data around in different ways:

– save data from a register to a memory location– load data from a memory location to a register– etc.

• Example instructions:– sw : store word– lw : load word

• Example usage:– sw $t1, 8($sp)– lw $t0, 4($sp)

Arithmetic Instructions• For performing calculations:

– adding, subtracting, multiplication, division, etc.

• Example Instructions:– add : add numbers in 2 registers and place in another register– sub : subtract the number in one register from the number in

another register and place in a 3rd register

• Example usage:– add $t0, $a0, $a1– sub $s0, $t0, $t1

Logical Instructions• For performing logical bitwise operations

– and-ing, or-ing, bitwise shifting, etc.

• Example Instructions:– add : bitwise ands the numbers in 2 registers and places the

result in a 3rd register– or : bitwise ors the numbers in 2 registers and places the

result in a 3rd register

• Example Usage:– and $0, $1, $2– or $0, $1, $2

Conditional Branch Instructions• For doing things like:

– if-type statements– controlling loops

• Example Instructions:– beq : if two registers have the same data, jump to instructions

at a provided memory address– bne : if two registers have different data, jump to the

instructions at a provided memory address

• Example Usage:– beq $a0, $s1 Equal_Case

Unconditional Branch Instructions• For jumping to specified instructions to:

– support conditional & iterative blocks of code– call methods– return from methods

• Example Instructions:– jr : jump to instructions in provided register

• Example usage:– jr $ra

So what are registers again?• Named, physical memory addresses in the CPU

• What are they for?– to hold data and instructions for CPU to use during program

execution

• Will an entire program’s data/instructions fit into a CPU’s registers?– not simultaneously– we will load and unload registers as needed

Registers vs. Variables• MIPS does not have variables

• You would never:– declare a variable– assign values to a variable– reference a variable

• Instead we use registers to store data

Registers store 32 bit patterns• You do not declare data types

• You do not name registers

• A register can be used to store an integer one moment and a real number the next

• When you put data in a register, you have to keep track of what data is where

How many registers do MIPS CPUs have?

• 32 for you to use– plus the Program Counter (PC)– plus the floating point registers

• Note, each register can be referenced via:– a number ($0 - $31)OR– a name (ex: $t0)

• Different types of registers are to be used for different purposes

MIPS Registers