Assembly Language II 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

Lecture 5: MIPS Instruction Set
CS/COE0447 Computer Organization & Assembly Language
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ELEN 468 Advanced Logic Design
Chapter 2 Instructions: Language of the Computer
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
CS2100 Computer Organisation MIPS Part III: Instruction Formats (AY2014/2015) Semester 2.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
CS325 Instructions: Language of the Machine MIPS ARCHITECTURE - AN INTRODUCTION TO THE INSTRUCTION SET by N. Guydosh 2/2/04+
MIPS Architecture CPSC 321 Computer Architecture Andreas Klappenecker.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
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.
Review for Midterm 1 CPSC 321 Computer Architecture Andreas Klappenecker.
The Processor 2 Andreas Klappenecker CPSC321 Computer Architecture.
1 Today  Finish-up procedures/stack  strlen example  Machine language, the binary representation for instructions. —We’ll see how it is designed for.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 1 In-Class Lab Session (Lab 2)
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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.
CMPE 325 Computer Architecture II
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
CENG 311 Instruction Representation
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Computer Architecture Instruction Set Architecture
COMPUTER ARCHITECTURE & OPERATIONS I
Computer Architecture Instruction Set Architecture
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture & Operations I
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Addressing in Jumps jump j Label go to Label op address 2 address
Solutions Chapter 2.
Computer Architecture & Operations I
MIPS Instructions.
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
Review.
Instruction encoding The ISA defines Format = Encoding
Instruction encoding The ISA defines Format = Encoding
Instruction encoding The ISA defines Format = Encoding
Computer Architecture
Presentation transcript:

Assembly Language II CPSC 321 Andreas Klappenecker

Any Questions?

Administrative Issues Confusion about Section 502 Your lab assignments should be turned in during your lab. Take advantage of the Teaching Assistant and the Peer Teacher. Learn about many computer related topics during the workshops offered by our graduate students. Go to seminars to learn about current trends in computer science.

The Story so far… We introduced numerous MIPS assembly language instructions. We are now familiar with registers and register usage conventions. We know how to use system calls, basic I/O We have learned how the stack works What is missing? Practice! Practice! Practice!

Stack 8($sp) 4($sp) 0($sp) high address low address stack pointer $sp -> The stack pointer is contained in register $sp. The stack grows from above. If you want to push a word onto the stack: $sp = $sp – 4 Efficiency: If you want to push 3 registers onto the stack, subtract 12! $sp = $sp - 12

Fibonacci Procedure We want to write a recursive procedure fib that performs the following calculation: If n=0 or n=1, then f(0)=0 and f(1)=1 simply return the argument If n>1, then f(n)=f(n-1)+f(n-2) recursively call fib

Top Down Design >= fib: > > We assume that $a0 contains the argument n, register $v0 the result, and $s0 some intermediate result.

Calculation >= bgt $a0,1, gen# if n>1, 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)

Save and Restore Registers >= subi $sp,$sp,12 sw $a0, 0($sp) sw $s0, 4($sp) sw $ra, 8($sp) >= lw $a0, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp,$sp, 12 jr $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

Quiz How do you calculate f(12) with the Fibonacci procedure that we have written? li $a0, 12 jal fib

Short Discussion You are now familiar with the basics of the MIPS assembly language. Start experimenting! Do it yourself! What kind of techniques do you use to design your programs? What style of documentation are you using? Are you familiar with TeX or LaTeX?

What Next? We need a more detailed knowledge about the instruction formats to fully appreciate certain restrictions. The functional interface is easy to understand, since it is basically familiar procedural programming We need to understand how the computer interprets the instruction, so that we can transition to the discussion of the MIPS hardware architecture

Machine Language Machine language level programming means that we have to provide the bit encodings for the instructions For example, add $t0, $s1, $s2 represents the 32bit string Assembly language mnemonics usually translate into one instruction We also have pseudo-instructions that translate into several instructions What does that mean?

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

Register Format (R-Format) Register format op: basic operation of instruction funct: variant of instruction rs: first register source operand rt: second register source operand rd: register destination operand shamt: shift amount op-code rs rt rd shamt funct

Watson, the case is clear… add $t0, $s1, $s Operation and function field tell the computer to perform an addition registers $17, $18 and $8 op-code rs rt rd shamt funct

0$zero 1$at 2$v0 3$v1 4$a0 5$a1 6$a2 7$a3 8$t0 9$t1 10$t2 11$t3 12$t4 13$t5 14$t6 15$t7 16$s0 17$s1 18$s2 19$s3 20$s4 21$s5 22$s6 23$s7 24$t8 NumberNameValue Registers pass parameters to functions return values from functions $s0-$s7 are callee-saved registers – use these registers for values that must be maintained across function calls. $t0-$t7 are caller saved registers – use these registers in functions

Watson, the case is clear… add $t0, $s1, $s source registers $s1=$17 and $s2=$18 and target register $t0=$8 op-code rs rt rd shamt funct

R-Format Example Register format (op, funct)=(0,32): add rs=17: first source operand is $s1 rt=18: second source operand is $s2 Rd=8: register destination is $t0 add $t0, $s1, $s

Immediate Format (I-Format) Immediate format op determines the instruction (op <> 0) rs is the source register rt is the destination register 16bit immediate value op rs rt immediate value

I-Format Example Immediate format op=8 means addi rs=29 means source register is $sp rt=29 means $sp is destination register immediate value = 4 addi $sp, $sp,

Problem The MIPS assembly language has the command andi, an immediate bit-wise and operation We can say li $s0, 0xCDEF1234 to load register $s0 with the content 0xCDEF1234 Why is this strange? In the immediate format, you can only load 16 bits, but the constant is 32 bits!

Pseudo-Instructions li $s0, 0xCDEF1234 is a pseudo-instruction It is a convenient shorthand for lui $at, 0xCDEF ori $s0, $at, 0x1234 The register $at is used here by the assembler; this is the reason why you should not use this register.

Puzzle How can we swap the content of two registers, say $s0 and $s1, without accessing other registers or memory? Solution: xor $s0, $s0, $s1 xor $s1, $s0, $s1 xor $s0, $s0, $s1

MIPS Addressing Modes Immediate addressing Register addressing Base displacement addressing PC-relative addressing address is the sum of the PC and a constant in the instruction Pseudo-direct addressing jump address is 26bits of instruction concatenated with upper bits of PC

Addressing Modes Register Addressing add $s1, $s2, $s3 $s1 = $s2 + $s3 Immediate Addressing addi $s1, $s2, 100 $s1 = $s

Addressing Modes Base addressing lw $s1, 100($s2) $s1 = Memory[$s2+100] PC-relative branch beq $s1, $s2, 25 if ($s1 == $s2) goto PC

Addressing Modes Pseudo-direct addressing j 1000 goto 1000 concatenate 26bit address with upper bits of the PC Study section 3.8 for further details In particular, get used to Figure 3.18

Conclusion Read Chapter 3 in conjunction with Appendix A You will miss many details – go back and read it again and again after we have discussed the MIPS hardware architecture. Now is the time to get familiar with assembly language programming! No cheating! Make sure that you program at least two procedures this weekend. Have a close look at the simulator. What does it do with pseudo-instructions?