mips and qtspim recap - walla walla universitycurt.nelson/cptr280/lecture/overview.pdfmips and...

14
1 Cptr280, Autumn 2017 MIPS and QTSPIM Recap Cptr280 Dr Curtis Nelson MIPS Architecture The MIPS architecture is considered to be a typical RISC architecture Simplified instruction set Programmable storage 32 x 32-bit General Purpose Registers (register 0 is a fixed value of 0) 2^32 bytes of addressable main memory

Upload: nguyencong

Post on 15-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

1Cptr280, Autumn 2017

MIPS and QTSPIM Recap

Cptr280

Dr Curtis Nelson

MIPS Architecture

• The MIPS architecture is considered to be a typical RISC architecture– Simplified instruction set

• Programmable storage– 32 x 32-bit General Purpose Registers (register 0 is a fixed value of 0)– 2^32 bytes of addressable main memory

Page 2: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

2Cptr280, Autumn 2017

MIPS-32 Instruction Set Architecture

R0 - R31

PCHILO

Registers

opop

op

rs rt rd sa functrs rt immediate

jump target

3 Instruction Formats: all 32 bits wide

R formatI format

J format

Register Names in MIPS Assembly Language

• With MIPS, there is a convention for mapping register names into general purpose register numbers.

name register number usage$zero 0 constant 0$v0-$v1 2-3 results$a0-$a3 4-7 arguments$t0-$t7 8-15 temporaries$s0-$s7 16-23 saved$t8-$t9 24-25 more temps$gp 28 global pointer$sp 29 stack pointer$fp 30 frame pointer$ra 31 return address

Page 3: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

3Cptr280, Autumn 2017

MIPS Arithmetic Instructions

• MIPS assembly language arithmetic instructions:add $t0, $s1, $s2sub $t0, $s1, $s2

§ Each arithmetic instruction performs one operation.

§ Each specifies exactly three operands that are all contained in the datapath's register file ($t0,$s1,$s2)

§ destination ¬ source1 op source2

§ Instruction Format (R format)

0 17 18 8 0 0x22

• MIPS fields are given names to make them easier to refer to:

MIPS Instruction Fields

op rs rt rd shamt funct

op 6-bits opcode that specifies the operationrs 5-bits register file address of the first source operandrt 5-bits register file address of the second source operandrd 5-bits register file address of the result’s destinationshamt 5-bits shift amount (for shift instructions)funct 6-bits function code augmenting the opcode

Page 4: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

4Cptr280, Autumn 2017

MIPS Memory Model

• Address space– 32 address bits meaning 232 unique

memory locations

MIPS Assembly Language Program Structure

• Just plain text file with data declarations, program code (name of file should end in suffix .s to be used with SPIM simulator)

• Data declaration section followed by program code section

Page 5: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

5Cptr280, Autumn 2017

Data Declarations

• Placed in section of program identified with assembler directive .data

• Declares variable names used in program; storage allocated in main memory (RAM)

Code

• Placed in section of text identified with assembler directive .text

• Contains program code (instructions) • Starting point for code execution given label main:• Ending point of main code should use exit system call

(covered later under System Calls)

Page 6: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

6Cptr280, Autumn 2017

Comments

• Anything following # on a line– # This stuff would be considered a comment

Template for a MIPS assembly language program

# Comment giving name of program and description of function# Template.s# Bare-bones outline of MIPS assembly language program

.data# variable declarations here# ...

.text

main: # indicates start of code (first instruction to execute)# remainder of program code here# ...# ...

Page 7: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

7Cptr280, Autumn 2017

Example Program: add2numbersProg1.asm

## Program adds 10 and 11

.text # text section

.globl main # call main by SPIM

main:

ori $8,$0,0xA # load “10" into register 8ori $9,$0,0xB # load “11" into register 9

add $10,$8,$9 # add registers 8 and 9, put result # in register 10

Code Translation Example – Assumes Variables Stored in Register File

if-then-else structure written in C:if (a < 0) then {

b = 0 – a;d++; }

else {b = a;e++;}

if-then-else structure written in MIPS assembly language:bgez $t8, elsesub $s0, $r0, $t8addi $t1, $t1, 1b next

else:mov $s0, $t8addi $t2, $t2, 1

next:

a is stored in: $t8b $s0d $t1e $t2

Page 8: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

8Cptr280, Autumn 2017

Code Translation Example – Assumes Variables Stored in Main Memory

QTSPIM Introduction

• What is SPIM?– a simulator that runs assembly programs for MIPS R2000/R3000

RISC computers

• What does SPIM do?– reads MIPS assembly language files and translates to machine

language– executes the machine language instructions– shows contents of registers and memory– works as a debugger (supports break-points and single-stepping)– provides basic Operating System (OS)-like services, like simple

I/O

Page 9: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

9Cptr280, Autumn 2017

Learning MIPS & SPIM

• MIPS assembly is a low-level programming language.• The best way to learn any programming language is from live code.• We will start by going through a few example programs and explaining

the key concepts.• Further, an examples directory has been created in

/wwu/class/cptr280 of several simple, well-documented assembly programs for you to experiment with.

• Line-by-line syntax can best be learned by picking up what you need from the textbook and on-line tutorials.

• Start by copying existing programs and modifying them incrementally making sure you understand the behavior at each step.

• Tip: The best way to understand and remember a construct or keyword is to experiment with it in code, not by reading about it.

Using SPIM

• Loading source file– Use File -> Open menu

• Simulation– Simulator -> Settings… :

• In the Display section check only the first two items: Save window positions and General registers in hexadecimal

• In the Execution section check only Allow pseudo instructions– Simulator -> Set Value… : to load PC with address of first instruction

• enter Address or Register Name as “PC” and enter Value as “0x00400000”– reason: the text area of memory, where programs are stored, starts here

– Simulator -> Go : run loaded program• Click the OK button in the Run Parameters pop-up window if the

StartingAddress: value is “0x00400000”– Simulator -> Break : stop execution– Simulator -> Clear Registers and Reinitialize : clean-up before new run

Page 10: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

10Cptr280, Autumn 2017

Using SPIM

– Simulator -> Reload : load file again after editing – Simulator -> Single Step or Multiple Step : stepping to debug– Simulator -> Breakpoints : set breakpoints

• Notes:– Text segment window of SPIM shows assembly and corresponding machine

code• pseudo-instructions each expand to more than one machine instruction

– If Load trap file is checked in Simulator -> Settings… then text segment shows additional trap-handling code

– If Delayed Branches is checked in Simulator -> Settings… then statementx will execute before control jumps to L1 in following code – to avoid this, insert nop before statementx:

nopjal L1statementx…

L1: …

SPIM Example Program: add2numbersProg1.asm

## Program adds 10 and 11

.text # text section

.globl main # call main by SPIM

main:

ori $8,$0,0xA # load “10" into register 8ori $9,$0,0xB # load “11" into register 9

add $10,$8,$9 # add registers 8 and 9, put result # in register 10

Page 11: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

11Cptr280, Autumn 2017

MIPS Assembly Code Layout

• Typical Program Layout

.data #data section

# user program data

.text #code section

.globl main #starting point: must be global

main:

# user program code

MIPS Assembler Directives

• Top-level Directives:

– .text• indicates that following items are stored in the user text segment,

typically instructions– .data

• indicates that following data items are stored in the data segment– .globl sym

• declare that symbol sym is global and can be referenced from other files

Page 12: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

12Cptr280, Autumn 2017

More MIPS Assembler Directives

• Common Data Definitions:

– .word w1, …, wn• store n 32-bit quantities in successive memory words

– .half h1, …, hn• store n 16-bit quantities in successive memory halfwords

– .byte b1, …, bn• store n 8-bit quantities in successive memory bytes

– .ascii str• store the string in memory but do not null-terminate it

– characters are represented in single quotes ‘a’– strings are represented in double-quotes “str”– special characters, e.g.. \n, \t, follow C convention

– .asciiz str• store the string in memory and null-terminate it

SPIM System Calls

• System Calls (syscall)– OS-like services

• Method– load system call code into register $v0 (see following table

for codes)– load arguments into registers $a0, …, $a3– call system with SPIM instruction syscall– after call, return value is in register $v0, or $f0 for floating

point results

Page 13: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

13Cptr280, Autumn 2017

SPIM System Call Codes

Service Code (put in $v0) Arguments Result

print_int 1 $a0=integer

print_float 2 $f12=float

print_double 3 $f12=double

print_string 4 $a0=address of string

read_int 5 int 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 addr in $v0

exit 10

SPIM Example Program: systemCalls.asm

## Enter two integers in

## console window

## Sum is displayed

.text

.globl main

main:

la $t0, value

li $v0, 5

syscall

sw $v0, 0($t0)

li $v0, 5

syscall

sw $v0, 4($t0)

lw $t1, 0($t0)

lw $t2, 4($t0)

add $t3, $t1, $t2

sw $t3, 8($t0)

li $v0, 4

la $a0, msg1

syscall

li $v0, 1move $a0, $t3

syscall

li $v0, 10

syscall

.data

value: .word 0, 0, 0msg1: .asciiz “Sum = "

system call codefor read_int

result returned by call

argument to print_string call

system call codefor print_string

system call codefor print_int

argument to print_int call

system call codefor exit

Page 14: MIPS and QTSPIM Recap - Walla Walla Universitycurt.nelson/cptr280/lecture/overview.pdfMIPS and QTSPIM Recap ... MIPS Assembly Language Program Structure ... • Line-by-line syntax

14Cptr280, Autumn 2017

More SPIM Example Programs• In the class examples directory you will find 18 simple well-documented MIPS

assembly programs. Run the code in the order below of increasing complexity.

1. add2numbersProg12. add2numbersProg23. storeWords4. swap2memoryWords5. branchJump6. systemCalls7. overflow8. averageOfBytes9. printLoop10. sumOfSquaresProg111. sumOfSquaresProg212. sumOfSquaresProg313. procCallsProg114. procCallsProg1Modified15. procCallsProg216. addFirst10017. factorialNonRecursive18. factorialRecursive

Conclusions

• The code presented so far should get you started in writing your own MIPS assembly.

• Remember the only way to master the MIPS assembly language – in fact, any computer language – is to write lots and lots of code.

• For anyone aspiring to understand modern computer architecture it is extremely important to master MIPS assembly as all modern computers (since the mid-80’s) have been inspired by, if not based fully or partly on the MIPS instruction set architecture.