MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.

Slides:



Advertisements
Similar presentations
Branches Two branch instructions:
Advertisements

Arrays First step is to reserve sufficient space for the array.
CDA 3100 Recitation Week 15. What does the function f1 do:.data A:.word 10,21,45,8,100,15,29,12,3,19 B:.word 2,5,33,5,20,1,53,52,5,5 C:.word 6,8,5,4,5,22,53,12,33,89.text.globl.
Comp Sci Control structures 1 Ch. 5 Control Structures.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
SPIM and MIPS programming
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Syscall in MIPS Xinhui Hu Yuan Wang.
MIPS Function Continued
MIPS Assembly Language Programming
Ch. 8 Functions.
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
Assembly Language Working with the CPU.
ECE 0142 Recitation #5.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
MIPS Assembly Language
Computer Architecture Lecture 2 Instruction Set Principles.
Computer Architecture Lecture 3 C and Assembly. Intro to Assembly Language MIPS and Intel Variables and Constants int count = 10, I, j, k; count.word.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Computer Architecture CPSC 321 Andreas Klappenecker.
Appendix A: MIPS Assembly Language. Instruction Format R-type I-type J-type.
Lab #1 PCSpim SPIM is a software simulator that loads and executes assembly language programs for the MIPS RISC computers. Download and install PCSpim.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 10: MIPS Simulator Today’s topic –SPIM Simulator Readings –Appendix B 1.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Architecture CSCE 350 Rabi Mahapatra Spring 2015.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
MIPS Architecture Topics –What resources MIPS assembly manipulates –CPU (Central Processing Unit) –ALU (Arithmetic & Logical Unit), Registers –Memory –I/O.
MIPS Coding. Exercise – the bubble sort 7/9/2016week04-3.ppt2.
CS 312 Computer Architecture & Organization
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Introduction to Lab #1 José Nelson Amaral.
CS2100 Computer Organisation
MIPS Coding Continued.
Computer Architecture & Operations I
MIPS instructions.
MIPS coding.
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Control Flow and Arrays
MPIS Instructions Functionalities of instructions Instruction format
Assembler Directives Example program: .data # DATA segment city: .asciiz “Seattle” .align 2 num: .word 2210, 2341, 26, 0x022ec,
MIPS Instruction Encoding
MIPS coding.
Computer Organization and Design Assembly & Simulation
MIPS Instruction Encoding
MIPS coding.
Компьютерийн зохион байгуулалт, ассемблер CS201
MIPS Coding.
MIPS function continued
MIPS Functions.
MIPS Coding.
Review.
MIPS Coding Continued.
MIPS coding.
Generalities for Assembly Language
MIPS Assembly Language Programming Computer Architecture
9/27: Lecture Topics Memory Data transfer instructions
CS 286 Computer Architecture & Organization
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
MIPS Functions.
Conditional Branching (beq)
Control Flow and Arrays
Presentation transcript:

MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker

MIPS Assembly Instructions add $t0, $t1, $t2 # $t0=$t1+$t2 sub $t0, $t1, $t2 # $t0=$t1-$t2 lw $t1, a_addr # $t1=Mem[a_addr] sw $s1, a_addr# Mem[a_addr]=$t1

Assembler directives.text assembly instructions follow.datadata follows.globlglobally visible label = symbolic address

Hello World!.text# code section.globl main main:li $v0, 4# system call for print string la $a0, str# load address of string to print syscall# print the string li $v0, 10# system call for exit syscall# exit.data str:.asciiz “Hello world!\n” # NUL terminated string, as in C

Addressing modes lw $s1, addr # load $s1 from addr lw $s1, 8($s0)# $s1 = Mem[$s0+8] register $s0 contains the base address access the address ($s0) possibly add an offset 8($s0)

Load and move instructions la $a0, addr# load address addr into $a0 li $a0, 12# load immediate $a0 = 12 lb $a0, c($s1)# load byte $a0 = Mem[$s1+c] lh $a0, c($s1) # load half word lw $a0, c($s1)# load word move $s0, $s1# $s0 = $s1

Control Structures Assembly language has very few control structures:  Branch instructions if cond then goto label  Jump instructions goto label We can build while loops, for loops, repeat-until loops, if-then-else structures from these primitives

Branch instructions beqz $s0, labelif $s0==0goto label bnez $s0, labelif $s0!=0 goto label bge $s0, $s1, labelif $s0>=$s1 goto label ble $s0, $s1, label if $s0<=$s1 goto label blt $s0, $s1, label if $s0<$s1 goto label beq $s0, $s1, label if $s0==$s1 goto label bgez $s0, $s1, label if $s0>=0 goto label

if-then-else structures if ($t0==$t1) then /* blockA */ else /* blockB */ beq $t0, $t1, blockA j blockB blockA: … instructions of then block … j exit blockB: … instructions of else block … exit:… subsequent instructions …

repeat-until loop repeat … until $t0>$t1 loop: … instructions of loop … ble $t0, $t1, loop # if $t0<=$t1 goto loop Other loop structures are similar… Exercise: Derive templates for various loop structures

System calls load argument registers load call code syscall li $a0, 10# load argument $a0=10 li $v0, 1# call code to print integer syscall# print $a0

SPIM system calls procedure code $v0 argument print int 1$a0 contains number print float 2$f12 contains number print double 3$f12 contains number print string 4$a0 address of string

SPIM system calls procedure code $v0 result read int 5res returned in $v0 read float 6res returned in $f0 read double 7res returned in $f0 read string 8

Example programs Loop printing integers 1 to 10 Increasing array elements by for(i=0; i<len; i++) { a[i] = a[i] + 5; }

main:li $s0, 1# $s0 = loop counter li $s1, 10# $s1 = upper bound of loop loop:move $a0, $s0# print loop counter $s0 li $v0, 1 syscall li $v0, 4# print “\n” la $a0, linebrk# linebrk:.asciiz “\n” syscall addi $s0, $s0, 1# increase counter by 1 ble $s0, $s1, loop# if ($s0<=$s1) goto loop li $v0, 10# exit syscall Print numbers 1 to 10

Increase array elements by 5.text.globl main main: la $t0, Aaddr # $t0 = pointer to array A lw $t1, len # $t1 = length (of array A) sll $t1, $t1, 2 # $t1 = 4*length add $t1, $t1, $t0 # $t1 = address(A)+4*length loop: lw $t2, 0($t0) # $t2 = A[i] addi $t2, $t2, 5 # $t2 = $t2 + 5 sw $t2, 0($t0) # A[i] = $t2 addi $t0, $t0, 4 # i = i+1 bne $t0, $t1, loop # if $t0<$t1 goto loop.data Aaddr:.word 0,2,1,4,5 # array with 5 elements len:.word 5

Increase array elements by 5.text.globl main main:la $t0, Aaddr# $t0 = pointer to array A lw $t1, len# $t1 = length (of array A) sll $t1, $t1, 2# $t1 = 4*length (byte addr.) add $t1, $t1, $t0# $t1 = beyond last elem. A

Increase array elements by 5 Loop:lw $t2, ($t0)# $t2 = A[i] addi $t2, $t2, 5# $t2 = $t2 + 5 sw $t2, ($t0)# A[i] = $t2 addi $t0, $t0, 4# i = i+1 bne $t0, $t1, loop# if $t0<$t1 goto loop li $v0, 10# exit syscall

Increase array elements by 5.data Aaddr:.word 0,2,1,4,5 len:.word 5 Idiosyncratic: Byte addressing => loop in steps of 4 Describe meaning of registers in your documentation!

Procedures jal addr store address + 4 into $ra jump to address addr jr $ra allows subroutine to jump back care must be taken to preserve $ra! more work for non-leaf procedures

Procedures one of the few means to structure your assembly language program small entities that can be tested separately can make an assembly program more readable recursive procedures

Write your own procedures # prints the integer contained in $a0 print_int: li $v0, 1 # system call to syscall # print integer jr $ra # return main:... li $a0, 10# we want to print 10 jal print_int # print integer in $a0

Write your own procedures.data linebrk:.asciiz “\n”.text print_eol: # prints "\n" li $v0, 4 # la $a0, linebrk # syscall # jr $ra # return main:... jal print_eol # printf(“\n”)

Write your own procedures.data main: li $s0, 1 # $s0 = loop ctr li $s1, 10 # $s1 = upperbnd loop: move $a0, $s0 # print loop ctr jal print_int # jal print_eol # print "\n" addi $s0, $s0, 1 # loop ctr +1 ble $s0, $s1, loop # unless $s0>$s1…

Non-leaf procedures Suppose that a procedure procA calls another procedure jal procB Problem: jal stores return address of procedure procB and destroys return address of procedure procA Save $ra and all necessary variables onto the stack, call procB, and retore

Stack 8($sp) 4($sp) 0($sp) high address low address stack pointer $sp --> The stack can be used for  parameter passing  storing return addresses  storing result variables  stack pointer $sp $sp = $sp - 12

Fibonacci fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) 0, 1, 1, 2, 3, 5, 8, 13, 21,…

Fibonacci li $a0, 10 # call fib(10) jal fib# move $s0, $v0 # $s0 = fib(10) fib is a recursive procedure with one argument $a0 need to store argument $a0, temporary register $s0 for intermediate results, and return address $ra

fib: sub $sp,$sp,12 # save registers on stack sw $a0, 0($sp) # save $a0 = n sw $s0, 4($sp) # save $s0 sw $ra, 8($sp) # save return address $ra bgt $a0,1, gen # if n>1 then goto generic case move $v0,$a0 # output = input if n=0 or n=1 j rreg # goto restore registers gen: sub $a0,$a0,1 # param = n-1 jal fib # compute fib(n-1) move $s0,$v0 # save fib(n-1) sub $a0,$a0,1 # set param to n-2 jal fib # and make recursive call add $v0, $v0, $s0 # $v0 = fib(n-2)+fib(n-1) rreg: lw $a0, 0($sp) # restore registers from stack lw $s0, 4($sp) # lw $ra, 8($sp) # add $sp, $sp, 12 # decrease the stack size jr $ra

Practice, practice, practice!!! Read Chapter 3 and Appendix A Write many programs and test them Get a thorough understanding of all assembly instructions Study the register conventions carefully