Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.

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
The University of Adelaide, School of Computer Science
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Lecture 5: Decision and Control CS 2011 Fall 2014, Dr. Rozier.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
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.
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/
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Thumb Instruction Set.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
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
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 8: Control, Procedures, and the Stack CS 2011 Fall 2014, Dr. Rozier.
Lecture 4: MIPS Instruction Set
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
9/29: Lecture Topics Conditional branch instructions
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
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.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Lecture 6: Branching CS 2011 Fall 2014, Dr. Rozier.
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.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
F28HS Hardware-Software Interface Lecture 11: ARM Assembly Language 5.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
Computer Architecture & Operations I
Writing Functions in Assembly
Lecture 5: Procedure Calls
Prof. Hsien-Hsin Sean Lee
Computer Architecture Instruction Set Architecture
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
Chapter 4 Addressing modes
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Writing Functions in Assembly
ECE/CS 552: Instruction Sets – MIPS
The University of Adelaide, School of Computer Science
ARM Assembly Programming
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Review.
10/4: Lecture Topics Overflow and underflow Logical operations
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Presentation transcript:

Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr. Rozier (UM)

COURSE PLAN FOR TODAY 1.Control Instructions 2.Basic Memory Access

CONTROL INSTRUCTIONS

Branching Branches allow us to transfer control of the program to a new address. – b ( ) – bl ( ) b start bl start

Branch (b) Branch, possibly conditionally, to a new address. beq If Z=1, branch Good practice to use bal instead of b.

Branch with link (bl) Branch, possibly conditionally, to a new address. – Before the branch is complete, store the PC in the LR. – Allows easy return from the branch. bleq If Z=1, branch, saving the PC

Branch with link (bl) How do we get back once we’ve saved the PC? mov pc, lr Moves the contents of the link register to the program counter.

Goto vs. Subroutine b – Allows us to transfer control of the program without returning. bl – Allows us to transfer control of the program, with returning.

Implementing If Statements C code: if (i == j) f = g+h; else f = g - h; ARM code cmp r0, Set flags via r0-r1 and discard bne Else add r2, r3, r2 = r3 + r4 bal Exit Else: sub r2, r3, r2 = r3 - r4 Exit:

Implementing Loop Statements C code: while (i < j) i += 1; ARM code Loop: cmp r0, r1 bge Exit add r0, r0, #1 bal Loop Exit: i < j? i=i+1 i<j Exit i>=j

What about a Case Statement? Say we have a case statement: switch(x) { case 0: foo(); break; case 1: bar(); break; case 2: baz(); break; case 3: qux(); break; }

Jump Tables Set up a portion of memory as such: If our case variable is stored in r0… – r0 << #2 is the index into our jump table of the function address. Memory LocationContents 0x???? + 0address of foo 0x???? + 4address of bar 0x???? + 8address of baz 0x???? + 12address of qux

Cases Statements as Jump Tables (assume r0 holds the switch variable) ldr r1, =jumptable ldr pc, [r1, r0, lsl #2] Wasn’t that easy?

Jump Table Exercise Convert the follow C-style code into ARMv6 instructions: x = getPrime(); switch(x) { case 1: firstPrime(); break; case 3: secondPrime(); break; case 5: thirdPrime(); break; case 7: fourthPrime(); break; default: notFirstFourPrimes(); }

Pseudo-Instructions Notice the use of: – ldr r0, =casetable – We saw this before in helloworld.s What is really going on here?

Pseudo-Instructions Code as we wrote it: Disasembled code: ldrr1,=string swi0 movr7,#1 swi0 0x8080ldrr1,[pc,#8] 0x8084svc0x0 0x8088movr7#1 0x808csvc0x0 0x8090muleqr1r4r0

This is weird… Let’s play with gdb… x/x 0x8090 0x8090 :0x x/x 0x x10094 :“Hello World!\nA\025”

Looking back at the assembled code… While at instruction 0x8080 p/x $pc 0x8080 ldrr1,[pc,#8] 0x8084svc0x0 0x8088movr7#1 0x808csvc0x0 0x8090muleqr1r4r0

Looking back at the assembled code… While at instruction 0x8080 p/x $pc+8 0x8088 0x8080ldrr1,[pc,#8] 0x8084svc0x0 0x8088movr7#1 0x808csvc0x0 0x8090muleqr1r4r0 The real value of $pc +4

Looking back at the assembled code… While at instruction 0x8080 p/x $pc+8 0x8088 p/x ($pc+8)+8 0x8090 0x8080ldrr1,[pc,#8] 0x8084svc0x0 0x8088movr7#1 0x808csvc0x0 0x8090muleqr1r4r0

So why does it show up as muleq? Representing instructions – Condition Field 0000 – EQ – 0000 | | 0 | 0 |????|????|????| 1001|???? mul r1, r4, r0 mul{ }{S} rd, rm, rs Cond000000ASRdRnRs1001Rm Instruc ???? 1001???? Hex Bin

So why does it show up as muleq? Representing instructions mul r1, r4, r0 mul{ }{S} rd, rm, rs mul 0001, 0100, 0000 Cond000000ASRdRnRs1001Rm Instruc ???? 1001???? Hex Bin

So what is this? Code as we wrote it: Disasembled code: ldrr1,=string swi0 movr7,#1 swi0 0x8080ldrr1,[pc,#8] 0x8084svc0x0 0x8088movr7#1 0x808csvc0x0 0x8090muleqr1r4r0

The problem with immediates The fact that instructions, AND all their arguments, must take up only 32 bits limits the size of immediates to 1 byte. – Range 0 – 255. – Hello world was in 0x10094 – PC was at 0x8088 – Max offset with immediate value? 0x xFF = 0x8187

Enter, the Literal Pool 0x8080ldrr1,[pc,#8] 0x8084svc0x0 0x8088movr7#1 0x808csvc0x0 0x Last instruction in basic block Literal Pool

Basic Blocks A basic block is a sequence of instructions with – No embedded branches (except at end) – No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks

So how do we use the Literal Pool? We’ve been using it all along. ldr r0, =string ldr r0, [pc, #8]

What about procedures? Implement the following: foo(int x) { x = x + 1 } main() { x = 1; foo(x); }

What about procedures? Implement the following: int foo(int x) { if (x < 5) x = foo(x+1); return(x) } main() { x = 1; x = foo(x); }

The problem with Procedures When we never branch back, who cares what state the registers are in? If we branch and return, we expect our registers to be in the state that we left them! – Procedures need to use registers too!

A Call Chain Image by David Thomas

Procedure Calling 1.Place parameters for procedure in registers 2.Transfer control to procedure 3.Procedure acquires storage 4.Procedure performs function. 5.Procedure places return value in appropriate register 6.Return control

The Stack Region of memory managed with stack discipline. Accessed with push and pop

The Stack

Procedure Call Example 0x000 0x004 0x008 push {lr} bl procedure_2 pop {pc} 0x100 0x104 0x108 0x10c 0x110 0x114 0x118 lr0x080 sp0x100 pcx

Procedure Call Example 0x000 0x004 0x008 push {lr} bl procedure_2 pop {pc} 0x1000x080 0x104 0x108 0x10c 0x110 0x114 0x118 lr0x080 sp0x104 pcx

Procedure Call Example 0x000 0x004 0x008 push {lr} bl procedure_2 pop {pc} 0x1000x080 0x1040x008 0x1080xa4f 0x10c0xfff 0x1100xcc4 0x1140xbeef 0x1180xdead lr0x008 sp0x104 pcx

Procedure Call Example 0x000 0x004 0x008 push {lr} bl procedure_2 pop {pc} 0x1000x080 0x1040x008 0x1080xa4f 0x10c0xfff 0x1100xcc4 0x1140xbeef 0x1180xdead lr0x080 sp0x100 pcx

Stack Frames Stack frames “belong” to a procedure. Store local variables here (they go out of scope automatically) Can communicate with other procedures with a stack frame.

Communicating with Procedures 0x100lr 0x104return 0 0x108return 1 0x10c 0x110 0x114 0x118 Caller sets up stack frame

Communicating with Procedures 0x100lr 0x104return 0 0x108return 1 0x10c 0x110 0x114 0x118 Callee stores values before return

Procedures and Register Use What if we want to use some registers in the procedure? Caller could have data in it!

Stack Discipline and ABI Stack Discipline is important Define an Application Binary Interface – How should procedures communicate? How should the be called? Consistency, following standards, is the key.

Conventions and Discipline Caller Caller saves temporary values in its frame before the call. Caller restores values in its frame after the call. Callee Callee saves temporary values in its frame before using. Callee restores values in its frame after using.

WRAP UP

For next time More on stacks, procedures, and calling.