CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Assembly Language (cont)

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

CENG 311 Decisions in C/Assembly Language
Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
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.
Systems Architecture Lecture 5: MIPS Instruction Set
Chapter 2 Instructions: Language of the Computer
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
Lecture 8: MIPS Instruction Set
1  1998 Morgan Kaufmann Publishers and UCB CEG3420 Computer Design Lecture 4 MIPS Instruction Set Philip Leong.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
EECC550 - Shaaban #1 Lec # 2 Winter Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of instructions.
Reduced Instruction Set Computer (RISC)
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
EECC550 - Shaaban #1 Lec # 2 Winter Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of instructions.
EECC550 - Shaaban #1 Lec # 2 Winter (Chapter 2) Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
EECC250 - Shaaban #1 lec #21 Winter Complex Instruction Set Computer (CISC) Emphasizes doing more with each instruction. Motivated by the high.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Fall EE 333 Lillevik 333f06-l5 University of Portland School of Engineering Computer Organization Lecture 5 MIPS Instructions Loops Machine Instructions.
Lecture 4: MIPS Instruction Set
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.
True Assembly Language Part I. The Assembly Process The process of translating a MAL code (an assembly language program) into machine code (a sequence.
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.
CMPE 325 Computer Architecture II
Intro & ISA.1 1/14 Instruction Set Architecture (ISA) (Ch: , th ed) Instructions: Language of the Computer Dr. Khaled El-Ayat.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization and Design Jair Gonzalez Enero 2004 MIPS ISA.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i == j ) h = i + j;
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 11 Conditional Operations.
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Architecture & Operations I
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Conditional Control Structure
MPIS Instructions Functionalities of instructions Instruction format
MISP Assembly.
How to represent signed integers
Computer Architecture & Operations I
ECE232: Hardware Organization and Design
Instruction encoding The ISA defines Format = Encoding
The University of Adelaide, School of Computer Science
Part II Instruction-Set Architecture
MIPS Assembly.
Flow of Control -- Conditional branch instructions
MIPS Assembly.
MIPS assembly.
7/6/
Conditional Control Structure
9/13/
Conditional Branching (beq)
Presentation transcript:

CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Assembly Language (cont)

CMPE 325 CH #3Slide #2 Loop Example Consider the code where array A is an integer array with 100 elements Loop:g = g + A[i] i = i + j; if (i != h) goto Loop: g:$s0 h:$s1 i:$s2 j:$s3 A:$s4

CMPE 325 CH #3Slide #3 Loop Solution Use a conditional test Loop:add $t0, $s2, $s2# $t0 = 2 * i add $t0, $t0, $t0# $t0 = 4 * i add $t1, $t0, $s4# $t1 = &(A[i]) lw $t2, 0($t1)# $t2 = A[i] add $s0, $s0, $t2# g = g + A[i] add $s2, $s2, $s3# i = i + j bne $s2, $s1, Loop# goto Loop if i!=h This sequence is known as a basic block since it has one entrance and one exit

CMPE 325 CH #3Slide #4 Loops in C Consider a very similar case with while while (A[i] == k) i = i + j; Use a similar loop as before Loop:add $t0, $s0, $s0# $t0 = 2 * i add $t0, $t0, $t0# $t0 = 4 * i add $t1, $t0, $s3# $t1 = &(A[i]) lw $t2, 0($t1)# $t2 = A[i] bne $t2, $s2, Exit# goto Exit if != add $s0, $s0, $s1# i = i + j j Loop# goto Loop Exit: What is wrong with this approach?

CMPE 325 CH #3Slide #5 Loop Efficiency Code uses two branches/iteration: Better structure: Cond? Body of loop Cond? Body of loop

CMPE 325 CH #3Slide #6 Improved Loop Solution Remove extra branch j Cond# goto Cond Loop:add $s0, $s0, $s1# i = i + j Cond:add $t0, $s0, $s0# $t0 = 2 * i add $t0, $t0, $t0# $t0 = 4 * i add $t1, $t0, $s3# $t1 = &(A[i]) lw $t2, 0($t1)# $t2 = A[i] beq $t2, $s2, Loop# goto Loop if == Exit: Reduced loop from 7 to 6 instructions Even small improvements important if loop executes many times

CMPE 325 CH #3Slide #7 Other Comparisons Other conditional arithmetic operators are useful in evaluating conditional expressions using, = Conditional expressions also useful in signed vs. unsigned integers (to be discussed later) Register is “set” to 1 when condition is met Consider the following code if (f < g) goto Less; Solution slt $t0, $s0, $s1# $t0 = 1 if $s0 < $s1 bne $t0, $zero, Less# Goto Less if $t0 != 0

CMPE 325 CH #3Slide #8 MIPS Comparisons InstructionExampleMeaningComments set less thanslt $1, $2, $3$1 = ($2 < $3) comp less than signed set less than immslti $1, $2, 100$1 = ($2 < 100) comp w/const signed set less than unssltu $1, $2, $3$1 = ($2 < $3) comp < unsigned set l.t. imm. unssltiu $1, $2, 100$1 = ($2 < 100) comp < const unsigned

CMPE 325 CH #3Slide #9 MIPS Jumps & Branches InstructionExampleMeaning jumpj Lgoto L jump registerjr $1goto value in $1 jump and linkjal Lgoto L and set $ra jump and link registerjalr $1goto $1 and set $ra branch equalbeq $1, $2, Lif ($1 == $s2) goto L branch not eqbne $1, $2, Lif ($1 != $2) goto L branch l.t. 0bltz $1, Lif ($1 < 0) goto L branch l.t./eq 0blez $1, Lif ($1 <= 0) goto L branch g.t. 0bgtz $1, Lif ($1 > 0) goto L branch g.t./eq 0bgez $1, Lif ($1 >= 0) goto L

CMPE 325 CH #3Slide #10 Simplicity Notice that there was no branch less than instruction for comparing two registers? The reason is that such an instruction would be too complicated and would force a longer clock cycle time Therefore, conditionals that are not comparing against zero take at least two instructions where the first is a set and the second is a conditional branch The MIPS assembler supports pseudoinstructions for such operators and automatically converts them to the appropriate sequence of MIPS instructions

CMPE 325 CH #3Slide #11 Pseudoinstructions Assembler expands pseudoinstructions move $t0, $t1# Copy $t1 to $t0 add $t0, $zero, $t1# Actual instruction Some pseudoinstructions need a temporary register Cannot use $t, $s, etc. since they may be in use The $at register is reserved for the assembler blt $t0, $t1, L1# Goto L1 if $t0 < $t1 slt $at, $t0, $t1# Set $at = 1 if $t0 < $t1 bne $at, $zero, L1# Goto L1 if $at != 0

CMPE 325 CH #3Slide #12 Pseudoinstructions (cont) The pseudoinstruction load immediate (li) provides transfer of a 16-bit constant value to reg li $t0, imm# Copy 16bit imm. value to $t0 addi $t0, $zero, imm# Actual instruction Example: Write a MIPS code to load 0 h lui $s0, 0  7654 # $s0 is set to 0  h addi $s0, $s0,0  3210 # After addition 0  h

CMPE 325 CH #3Slide #13 Logical Operators Bitwise operators often useful for converting &&, ||, and ! symbols into assembly Always operate unsigned (more later)

CMPE 325 CH #3Slide #14 MIPS Logical Instructions InstructionExampleMeaningComments and and $1, $2, $3$1 = $2 & $3Logical AND oror $1, $2, $3$1 = $2 | $3Logical OR xorxor $1, $2, $3$1 = $2 $3Logical XOR nornor $1, $2, $3$1 = ~($2 | $3)Logical NOR and immediateandi $1, $2, 10$1 = $2 & 10Logical AND w. constant or immediateori $1, $2, 10$1 = $2 | 10Logical OR w. constant xor immediate xori $1, $2, 10 $1 = ~$2 & ~10Logical XOR w. constant shift left logsll $1, $2, 10$1 = $2 << 10Shift left by constant shift right logsrl $1, $2, 10$1 = $2 >> 10Shift right by constant shift right arithsra $1, $2, 10$1 = $2 >> 10Shift right (sign extend) shift left log varsllv $1, $2, $3$1 = $2 << $3 Shift left by variable shift right log varsrlv $1, $2, $3 $1 = $2 >> $3 Shift right by variable shift right arithsrav $1, $2, $3 $1 = $2 >> $3 Shift right arith. by var load upper imm lui $1, 40 $1 = 40 << 16Places imm in upper 16b

CMPE 325 CH #3Slide #15 Handling Procedures Procedures are useful for decomposing applications into smaller units Implementing procedures in assembly requires several things to be done Space must be set aside for local variables Arguments must be passed in and return values passed out Execution must continue after the call

CMPE 325 CH #3Slide #16 Procedure Steps 1. Place parameters in a place where the procedure can access them 2. Transfer control to the procedure 3. Acquire the storage resources needed for the procedure 4. Perform the desired task 5. Place the result value in a place where the calling program can access it 6. Return control to the point of origin

CMPE 325 CH #3Slide #17 Stacks Stacks are a natural way to temporarily store data for procedures (as well as call/return information) since they have a LIFO behavior Data is pushed onto the stack to store it and popped from the stack when not longer needed Implementation Common rules across procedures required Recent machines are set by software convention and earlier machines by hardware instructions

CMPE 325 CH #3Slide #18 Using Stacks Stacks can grown up or down Stack grows down in MIPS Entire stack frame is pushed and popped, rather than single elements

CMPE 325 CH #3Slide #19 Calling a Procedure To jump to a procedure, use the jal jump-and- link instruction jal tartget# Jump and link to label  Saves the address of the next location into to register-31 and  Jumps to the specified 26-bit local word address

CMPE 325 CH #3Slide #20 jal and jr Instructions jal Prod_Addr (J-Type) first increments the program counter (PCPC+4), so that it points to the next location, then it stores that value into $ra (= $31). jr $ra (R-Type) copies $ra into PC, PC  $ra causes to jump back to the stored return address. 3Prod_Addr/

CMPE 325 CH #3Slide #21 Who saves the register? Caller save All values that have to be kept must be saved before a procedure is called. Callee save Within the procedure all used registers are saved and afterwards restored.

CMPE 325 CH #3Slide #22 Argument Passing Convention Return value is transferred in $v0…$v1. ($v0 and $v1) corresponds to ($2 and $3) Integer arguments up to four are passed in registers $a0 … $a3. (= $4 … $7). Any higher data structure is passed by a pointer. If there are more than 4 parameters, first four parameters are passed in registers, the others are transferred in the stack

CMPE 325 CH #3Slide #23 Register Saving Conventions Save the registers saved-registers s0…s7, arguments a0 … a3, and system registers $gp, $sp, $fp and $ra before they are corrupted in a call. Restore them before the start of calling code.

CMPE 325 CH #3Slide #24 Procedure Coding Example The C code for swap procedure is: swap(int v[ ], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } Code it in MIPS Assembly.

CMPE 325 CH #3Slide #25 Coding Example -swap Allocate registers to procedure variables. swap(int v[], int k)  two arguments pointer v[ ] in $a0, integer k in $a1. $t0, $t1, … for temp. values and addresses Write MIPS code for the procedure body. sll $t1, $a1, 2# $t1  k  4 add $t1, $a0, $t1# $t1  v+(k  4) lw$t0, 0($t1)# $t0 = v[k] lw$t2, 4($t1)# $t2 = v[k+1] sw$t2, 0($t1)# v[k]  $t2 ( = v[k+1] ) sw$t0, 4($t1)# v[k+1]  $t0 (= v[k] ) Nothing to save since only temp.regs corrupted. none of {$s0…$s7, $a0 …$a3, $sp, $ra} is corrupted swap(int v[ ], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; }

CMPE 325 CH #3Slide #26 Swap in Final Form Final form of the procedure swap: sll $t1, $a1, 2# $t1  k  4 add $t1, $a0, $t1# $t1  v+(k  4) lw$t0, 0($t1)# $t0 = v[k] lw$t2, 4($t1)# $t2 = v[k+1] sw$t2, 0($t1)# v[k]  $t2 ( = v[k+1] ) sw$t0, 4($t1)# v[k+1]  $t0 (= v[k] ) jr $ra Label to call the procedure return to caller code

CMPE 325 CH #3Slide #27 Nested Calls A call in another call corrupts $ra. $ra must be saved on the stack before the second call and then restored after the return from the inner call.

CMPE 325 CH #3Slide #28 Nested Stacks The stack grows and shrinks downward A: A CALL B B: A B CALL C A B C C: RET A B A

CMPE 325 CH #3Slide #29 Coding Example: Procedures Assume x[ ] starts from and y[ ] starts from Code the following program in MIPS Assembler starting main from address 300, and f from 400. Main pocedure..... j=5 f(j,x); x[3]+=5;..... f procedure void f(int j, int x[]) { if (j= =7) x[2]=6*x[1]–x[0]; else g(y); } g procedure int g(int y[]) { int i; for (i=2;i<12;i++) y[i]*=2; y[2] – = 4; }

CMPE 325 CH #3Slide #30 Coding Example –Main Body Code for Main Main:... li $a0,5 li $a1,10000 jal f lw $t0,12($a1)# $t0  x[3] addi $t0,$t0,5 # $10  x[3] + 5 sw $t0,12($a1)# x[3]  x[3] + 5 … Main pocedure..... j=5 f(j,x); x[3]+=5;..... allocate registers two arguments in f(),  use $a0, $a1 one argument in g()  use $a0 j and x are saved variables in g(), i is local, temporary variable. address calculations in temp.registers.

CMPE 325 CH #3Slide #31 Coding Example - Procedure f() li $t0,7# if (j !=7 ) bne $a0, $t0, Else # go to Else. li $t1, 6 # $t1  6, lw $t2, 4($a1)# $t2  x[1] mult $t2, $t1# LO  6  x[1] mflo $t2# $t2  6  x[1] lw $t1, 0($a1)# $t1  x[0] sub $t1, $t2, $t1 # $t1  6  x[1] – x[0]; sw $t1, 8($a1) # x[2]  6  x[1] – x[0]; j ExitIf Else: li $a0, jal g ExitIf: f procedure void f(int j, int x[]) { if (j= =7) x[2]=6*x[1]–x[0]; else g(y); } $a0 is corrupted $ra is corrupted Callee saves the registers Callee restores the registers jr $ra return of procedure f() 2-words = 8 bytes f: label of procedure addi $sp,$sp, -8 sw $a0, 0($sp) sw $ra, 4($sp) lw $a0, 0($sp) lw $ra, 4($sp) addi $sp,$sp, 8

CMPE 325 CH #3Slide #32 Coding Example – Procedure g() g() procedure void g(int y[]) { int i; for (i=2;i<12;i++) y[i]*=2; y[2] – = 4; } g: li $t0, 2# $10= i  2 for loop Loop: slti $t1, $t0, 12# if ( i < 12) then $t1  1 beq $t1,$0, ExitF# if ($t1=0) exit for-loop. sll $t2, $t0, 2# $t2  4  i add $t2, $a0, $t2# $t2  y[] + 4  i lw $t3, 0($t2)# $t3  y[i] add $t3, $t3,$t3# $t3  2  y[i] sw $t3, 0($t2)# y[i]  2  y[i] addi $t0, $t0, 1# $t0 = i  i+1 j Loop# loop again ExitF:# end of for loop lw $t3, 8($a0)# $t3  y[2] addi $t3, $t3,–4# $t3  y[2] – 4 sw $t3, 8($a0)# y[2]  y[2] – 4 jr $31 Only temporary registers are used No registers need to be saved.

CMPE 325 CH #3Slide #33 Decimal Coding of a Program coding starts from memory address 300 ten. address memory word contents (instruction fields) assembly with pseudocode assembly without pseudocode opcrsrtrdsafn 300:8045li $a0,5add $4,$0,5 304: li $a1,10000add $5,$0, :3100jal f 312:355812lw $t0,12($a1)lw $8,12($5) 316:8885addi $t0,$t0,5addi $8,$8,5 320:435812sw $t0,12 ( $a1 ) sw $8,12 ( $5 ) ….……………… F: 400:829 –8addi $sp,$sp,–8addi $29,$29,–8 404:432940sw $a0,0 ( $sp ) sw $4,0 ( $29 ) 408: sw $ra,4 ( $sp ) sw $31,4 ( $29 ) 412:8087li $t0,7addi $8,7 416:5488bne $4,$t0,Elsebne $4,$8,Else 420:8096li $t1,6addi $9,$0,6 424:355104lw $t2,4 ( $a1 ) lw $10,4 ( $5 ) 428: mult $t2,$t1mult $10,$9 432: mflo $t2mflo $10 436:35590lw $t1,0($a1)lw $9,0($5) 440: sub $t1,$t2,$t1sub $9,$10,$9 444:43598sw $t1,8($a1)sw $9,8($5) 448:2115j ExitIf Else: 452: li $a0,20000addi $4,$0, :3119jal G ExitIf: 460:352940lw $a0,0($sp)lw $4,0($29) 464: lw $ra,4($sp)lw $31,4($29) 468:829 8addi $sp,$sp,8addi $29,$29,24 472: jr $rajr $31 100= 400/4 8= (392–360)/4 115= 460/4 119= 476/4

CMPE 325 CH #3Slide #34 Decimal Coding of a Program-2 continuation of coding address memory word contents (instruction fields) assembly with pseudocode assembly without pseudocode opcrsrtrdsafn g: 476:8082li $t0,2addi $8,$0, 2 Loop: 480:108912slti $t1,$t0,12slti $9,$8,12 484:4097beq $t1,$0, ExitFbeq $9,$0, ExitF 488: sll $t2,$t0,2sll $10,$8,2 492: add $t2,$t2,$a0add $t2,$t2,$4 496: lw $t3,0($t2)lw $11,0($10) 500: add $t3,$t3,$t3add $11,$11,$11 504: sw $t3,0($t2)sw $11,0($10) 508:8881addi $t0,$t0,1addi $8,$8,1 512:2120j Loop ExitF: 516:354138lw $t3, 8($a0)lw $11, 8($4) 520:811 –4addi $t3,$t3,–4addi $11,$11,–4 524:434118sw $t3, 8($a0)sw $11, 8($4) 528: jr $rajr $ =( )/4 120 =480/4