Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.

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

Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Branches Two branch instructions:
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
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.
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.
MIPS Assembly Language Programming
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
The University of Adelaide, School of Computer Science
Assembly Language Working with the CPU.
CS3350B Computer Architecture Winter 2015 Lecture 4
Lecture 8: MIPS Instruction Set
SPIM : A MIPS Simulator Archi & Net Lab 이용석
Fall 2003SYCS-401 Operating Systems Instruction Set Architecture An overview of MIPS R3000 assembly language.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
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 */
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Computer Organization and Architecture (AT70.01) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: Freely.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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
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.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
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.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Addressing Modes. Register Addressing Immediate Addressing Base Addressing Indexed Addressing PC-Relative Addressing.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 312 Computer Architecture & Organization
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
MIPS Instruction Set Advantages
CS2100 Computer Organisation
The University of Adelaide, School of Computer Science
Pick up the handout on your way in!!
MIPS Functions.
MIPS coding.
Instruction encoding The ISA defines Format = Encoding
MIPS Coding.
MIPS function continued
Lecture 6: Assembly Programs
Program Assembly.
9/27: Lecture Topics Memory Data transfer instructions
CS 286 Computer Architecture & Organization
MIPS function continued
Conditional Branching (beq)
Presentation transcript:

Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU

Outline Useful tables SPIM program examples Array, control, and expression Debug Homework bonus Mid-term Exam

Table of MIPS registers

Table of system services Set $v0

MIPS Assembler Directives Data Types.word,.half - 32/16 bit integer.byte - 8 bit integer (similar to ‘char’ type in C).ascii,.asciiz - string (asciiz is null terminated) Strings are enclosed in double-quotas( ” ) Special characters in strings follow the C convention newline (\n), tab (\t), quote (\”) newline (\n), tab (\t), quote (\”).double,.float - floating point.text - Indicates that following items are stored in the user text segment.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

MIPS instruction set MIPS instructions Name Addadd Subtractsub Add immediate addi Load word lw Store word sw Load byte lb Store byte sb Load upper immediate lui Branch on equal beq Branch on not equal bne Set less than slt Set less than immediate slti MIPS instructions NameJumpj Jump register jr Jump and link jal Movemove Multiplymult Multiply immediate multi Load immediate li Branch less than blt Branch less than or equal ble Branch greater than bgt Branch greater than or equal bge

Arrays /* Address calculation in assembler: */ /* Address calculation in assembler: */ address of A [x] = address of A [0] + (x * sizeof (element of A)); address of A [x] = address of A [0] + (x * sizeof (element of A)); # $t0 = address of start of A. # $t0 = address of start of A. # $t1 = n. # $t1 = n. mul $t2, $t1, 4 # compute offset from the start of the array mul $t2, $t1, 4 # compute offset from the start of the array # assuming sizeof(element)=4 # assuming sizeof(element)=4 add $t2, $t0, $t2 # add the offset to the address of A [0]. add $t2, $t0, $t2 # add the offset to the address of A [0]. # now $t2 = &A [n]. # now $t2 = &A [n]. sw $t3, ($t2) # A [n] = whatever is in $t3. sw $t3, ($t2) # A [n] = whatever is in $t3. lw $t3, ($t2) # $t3 = A [n]. lw $t3, ($t2) # $t3 = A [n].

SPIM Program Example I A Simple Program #sample example 'add two numbers’.text # text section.globl main# call main by SPIM main:la $t0, value# load address ‘value’ into $t0 lw $t1, 0($t0)# load word 0(value) into $t1 lw $t2, 4($t0)# load word 4(value) into $t2 add $t3, $t1, $t2# add two numbers into $t3 sw $t3, 8($t0)# store word $t3 into 8($t0).data# data section value:.word 10, 20, 0 # data for addition

SPIM Program Example II A Program with System Call #sample example 'system call'.text.globl main main: la $t0, value li $v0, 5# read_integer syscall syscall sw $v0, 0($t0) sw $v0, 0($t0) li $v0, 5# read_integer li $v0, 5# read_integer syscall syscall sw $v0, 4($t0) sw $v0, 4($t0) lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0) li $v0, 4# print_string li $v0, 4# print_string la $a0, msg1 la $a0, msg1 syscall syscall li $v0, 1# print_integer li $v0, 1# print_integer move $a0, $t3 #pseudoinstructions move $a0, $t3 #pseudoinstructions syscall syscall.data value:.word 0, 0, 0 msg1:.asciiz "Result = "

SPIM Program Example III A Program with Procedure Call # sample example ‘swap two numbers’.text.globlmain main: la$a0, array addi$a1, $0, 0 addi$sp, $sp, -4 sw$ra, 0($sp) jalswap lw$ra, 0($sp) addi$sp, $sp, 4 jr$ra.data array:.word 5, 4, 3, 2, 1.text #swap(int v[], int k) #{ #int temp; #temp = v[k]; #v[k] = v[k+1]; #v[k+1] = temp; #} swap:add$t1, $a1, $a1 add$t1, $t1, $t1 add$t1, $a0, $t1 lw$t0, 0($t1) lw$t2, 4($t1) sw$t2, 0($t1) sw$t0, 4($t1) jr$ra

Control statements (if, if-else) if ( condition ) { statements statements} # MIPS code for the # condition expression. beqz $t0, if_end_label # MIPS code for the statements. if_end_label: if ( condition ) { if-statements if-statements } else { else-statements} # MIPS code for the # condition expression. beqz $t0, if_else_label # MIPS code for the if-statements. j if_end_label if_else_label: # MIPS code for the else-statements. if_end_label:

Example A Program with “if” if ( i == j ) go to L1; f = g + h; L:f = f – i; # $s0 = f; $s1 = g; $s2 = h # $s3 = i; $s4 = j; beq $s3, $s4, L1# go to L1 if i equals j add $s0, $s1, $s2# f = g + h ( skipped if i equals j ) L:sub $s0, $s0, $s3# f = f – i ( always executed )

Example A Program with “if-else” if ( i == j ) f = g + h; else f = g - h; # $s0 = f; $s1 = g; $s2 = h # $s3 = i; $s4 = j; bne $s3, $s4, Else# go to Else if i  j add $s0, $s1, $s2# f = g + h ( skipped if i  j ) j Exit# go to Exit Else:sub $s0, $s1, $s2# f = g – h ( skipped if i = j ) Exit:

Control statements (while) while ( condition ) { while ( condition ) { statements statements }while_start_label: # MIPS code for the condition expression beqz $t0, while_end_label # MIPS code for the statements. j while_start_label while_end_label: Break ----> j while_end_label Continue ----> j while_start_label

Control statements (do-while) do { statements } while ( condition ); do_start_label: # MIPS code for the statements. do_cond_label: # MIPS code for the condition expr: beqz $t0, do_end_label j do_start_label do_end_label: Break---> j do_end_label Continue---> j do_cond_label

Example A Program with “while” while ( save[i] == k ) i = i + j; # $s3 = i; $s4 = j; $s5 = k # $s6 = address of save[0] Loop:add $t1, $s3, $s3# Temp reg $t1 = 2 * i add $t1, $t1, $t1# Temp reg $t1 = 4 * i add $t1, $t1, $s6# $t1 = address of save[i] lw $t0, 0($t1)# Temp reg $t0 = save[i] bne $t0, $s5, Exit# go to Exit if save[i]  k add $s3, $s3, $s4# i = i + j j Loop Exit:

Control statements (for) for ( init ; condition ; incr ) { for ( init ; condition ; incr ) { statements statements} # MIPS code for the init expression. for_start_label: # MIPS code for the condition expression beqz $t0, for_end_label # MIPS code for the statements. for_incr_label: # MIPS code for the incr expression. j for_start_label for_end_label: Break ---> j for_end_label Continue---> j for_incr_label

Control statements (switch) switch ( expr ) { switch ( expr ) { case const1: statement1 case const2: statement2... case constN: statementNdefault:default-statement} # MIPS code to compute expr. # Assume that this leaves the value in $t0. beq $t0, const1, switch_label_1 beq $t0, const2, switch_label_2... beq $t0, constN, switch_label_N # If there is a default, then add b switch_default # Otherwise, add the following line instead: b switch_end_label switch_label_1: # MIPS code to compute statement1. switch_label_2: # MIPS code to compute statement2....switch_label_N: # MIPS code to compute statementN. # If there's a default: switch_default: # MIPS code to compute default-statement. switch_end_label: Break ---> b switch_end_label

Example A Program with “switch” switch ( k ) { case 0: f = i + j; break; /* k = 0 */ case 1: f = g + h; break; /* k = 1 */ case 2: f = g - h; break; /* k = 2 */ case 3: f = i - j; break; /* k = 3 */ } # $s0 = f; $s1 = g; $s2 = h; # $s3 = i; $s4 = j; $s5 = k; # $t2 = 4; slt $t3, $s5, $zero# Test if k < 0 bne $t3, $zero, Exit# if k < 0, go to Exit slt $t3, $s5, $t2# Test if k < 4 beq $t3, $zero, Exit# if k >= 4, go to Exit add $t1, $s5, $s5# Temp reg $t1 = 2 * k add $t1, $t1, $t1# Temp reg $t1 = 4 * k add $t1, $t1, $t4# $t1 = address of JumpTable[k] lw $t0, 0($t1)# Temp reg $t0 = JumpTable[k] jr $t0# jump based on register $t0 L0:add $s0, $s3, $s4# k = 0 so f gets i + j j Exit# end of this case so go to Exit L1:add $s0, $s1, $s2# k = 1 so f gets g + h j Exit# end of this case so go to Exit L2:sub $s0, $s1, $s2# k = 2 so f gets g - h j Exit# end of this case so go to Exit L3:sub $s0, $s3, $s4# k = 3 so f gets i - j Exit:# end of switch statement

Expression evaluation (and) cond1 && cond2 cond1 && cond2 # MIPS code to compute cond1. # Assume that this leaves the value in $t0. # If $t0 is zero, we're finished (and the result is FALSE). beqz $t0, and_end # MIPS code to compute cond2. # Assume that this leaves the value in $t0. and_end:

Expression evaluation (or) cond1 || cond2 cond1 || cond2 # MIPS code to compute cond1. # Assume that this leaves the value in $t0. # If $t0 is not zero, we‘re finished (and the result is TRUE). bnez $t0, or_end # MIPS code to compute cond2. # Assume that this leaves the value in $t0. or_end:

Debug Three kinds of error Syntax error Syntax error Your program will not compile Run error Run error Your program will compile but will crash or hang Logical error Logical error Your program will run successfully but not do what you intend

Debug syntax error # add.s.text.global main main:la $t0, value lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 8($t0).data value:.word 10, 20, 0.globl When loading …

Debug run error Set break points

Debug run error Set break points

Debug run error Set break points

Debug logical error Review the program Run the program step by step, and watch the dynamics of registers

Homework bonus Practice MIPS procedures Chap3: ( tail recursion ) Chap3: ( tail recursion ) Due: 5:00 PM, 11/25/2003 Due: 5:00 PM, 11/25/2003 Save as ID_1.s and ID_2.s, and send to Save as ID_1.s and ID_2.s, and send to The sample code of answers will be online after two weeks

Mid-term Exam 日期: 11/11 9:10 AM ~ 11:00 分數分配座位 EE R143: 單數座號 EE R143: 單數座號 EE R105: 雙數座號 EE R105: 雙數座號