MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Goal: Write Programs in Assembly
The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
1 ECE369 ECE369 Chapter 2. 2 ECE369 Instruction Set Architecture A very important abstraction –interface between hardware and low-level software –standardizes.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
ELEN 468 Advanced Logic Design
SPIM and MIPS programming
Computer Architecture CSCE 350
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
MIPS Assembly Language
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Assembly Language II CPSC 321 Andreas Klappenecker.
1 Warning! Unlike the previous lessons, for today's lesson you will have to listen, think and even understand (for the exam, of course). Individuals with.
MIPS Architecture CPSC 321 Computer Architecture Andreas Klappenecker.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
Computer Architecture CPSC 321 Andreas Klappenecker.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CHAPTER 6 Instruction Set Architecture 12/7/
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Architecture CSCE 350 Rabi Mahapatra Spring 2015.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
MIPS Assembly.
MIPS Assembly Language Programming
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Assembly Programming using MIPS R3000 CPU
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MIPS Instructions.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Review.
COMS 361 Computer Organization
Computer Architecture Procedure Calls
Assembly Programming using MIPS R3000 CPU
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Instruction Set Architecture
Presentation transcript:

MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker

MIPS The MIPS assembly language has arithmetic instructions (add, sub,…) logical instructions (and, or,…) branch instructions (bgtz, bne, …) jump instructions (j, jr, …) load and store instructions (lb, sw,…) floating point instructions

MIPS Instructions and Registers The operands to arithmetic instructions cannot be arbitrary expressions like in high-level languages, but have to be registers. add $t0, $t1, $t2 # $t0=$t1+$t2 Registers are special locations of memory that are also visible to the programmer. The size of a register is 32 bits in the MIPS architecture.

MIPS Design Principles 1.Simplicity favors regularity (instructions have exactly three operands) 2.Smaller is faster (do not provide too many registers) 3.Good design demands good compromises (all instructions have the same length, but three different formats)

MIPS Datapath

Instruction Word Formats Register format Immediate format Jump format op-code rs rt rd shamt funct op-code rs rt immediate value op-code 26 bit current segment address

Instruction Format Examples Register Format add $t0, $t1, $t2 Immediate Format addi $t0, $t1, 10 Jump Format j label

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

Memory Organization 0x x x7fff ffff Reserved Stack segment Dynamic data Static data Text segment Data segment The memory is byte addressed. If you want to step word by word through the memory, then you have to increase your “pointer” each time by 4.

Addressing modes lw $s1, 8($s0)# $s1 = Mem[$s0+8] The register $s0 contains the base address. We can access the address by ($s0) and add an offset 8($s0) ; this will be useful for procedures that have to save registers on the stack.

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

MIPS Registers The MIPS architecture has 32 registers with 32 bits each, $0,…,$31. The MIPS assembly language provides more convenient symbolic names. The register usage follows certain conventions that you need to memorize.

MIPS Registers $zero contains the constant 0 $at is reserved for the assembler – do not use $v0 and $v1 contain results of procedure call $a0,…,$a3 contain arguments of procedure calls $t0,…,$t9 temporary registers (which are not preserved after a procedure call; caller needs to save these registers) $s0,…,$s7 temporary registers (by convention preserved after procedure call; callee needs to save these registers) $k0, $k1 reserved – do not use $gp, $sp, $fp global area, stack pointer, frame pointer $ra return address (used by a procedure call) We come back to these conventions!

Register Conventions Suppose you store the value 7 in $s3 and call a procedure. Does $s3 still contain 7 after the procedure call? Yes, it should (if the procedure is using $s3, then it is supposed to save its value, and has to restore the value of $s3 before returning) Suppose you store the value 7 in $t3 and call a procedure. Does $t3 still contain 7 after the procedure call? No, not necessarily. The variable $t3 is temporary and the procedure is allowed to overwrite this register and does not have to restore its value.

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 Procedures are one of the few means to structure your assembly language program Program small entities that can be tested separately Procedures can make an assembly program more readable Recursive procedures sometimes elegantly solve problems.

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 What kind of problem occurs? Problem: jal stores the return address of procedure procB and destroys return address of procedure procA Save $ra and all necessary variables onto the stack, call procB, and then restore.

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 The stack pointer is contained in register $sp. $sp = $sp - 12

Memory Organization 0x x x7fff ffff Reserved Stack segment Dynamic data Static data Text segment Data segment The memory is byte addressed. If you want to step word by word through the memory, then you have to increase your “pointer” each time by 4.

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

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