COMP3221 lec13-decision-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 13: Making Decisions in C/Assembly Language - I

Slides:



Advertisements
Similar presentations
CENG 311 Decisions in C/Assembly Language
Advertisements

Goal: Write Programs in Assembly
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
COMP3221 lec10-logical-II&mul.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 10: C/Assembler Logical and Shift – II & Multiplication.
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
cs61c L5 Decisions 9/13/00 1 CS61C - Machine Structures Lecture 5 - Decisions in C/Assembly Language September 13, 2000 David Patterson
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
CS61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Cs 61C L3 Decisions.1 Patterson Spring 99 ©UCB CS61C Constants and Making Decisions in C/Assembly Language Lecture 3 January 27, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Memory Access March,
CS61C L10 Introduction to MIPS : Decisions II (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
COMP3221 lec13-decision-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 4/5: Making Decisions in C/Assembly Language - I
COMP3221 lec14-decision-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 14: Making Decisions in C/Assembly Language – II.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
CS61C L7 MIPS Decisions (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Decisions in C/Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
CS61C L7 MIPS: Load & Store, Decisions (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
CS61C L7 MIPS Decisions (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #7 – MIPS.
CS 61C L07 MIPS Memory (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #7: MIPS Memory & Decisions.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
CS1104 Assembly Language: Part 1
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
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.
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Intel has developed a new technique to generate random numbers that is suitable for integration directly into the CPU! This circuit can turn out 2.4 billion.
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
COMP3221 lec04--prog-model.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Programmer’s Model of Microprocessors
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Making Decision – Microprocessor
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Lecture 15: 10/24/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
IT 251 Computer Organization and Architecture More MIPS Control Instructions Chia-Chi Teng.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Topic 7: Control Flow Instructions CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Computer Organization Instructions Language of The Computer (MIPS) 2.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
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.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
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.
MIPS: Load & Store, Decisions. Memory management review Three 3’s Picking blocks off free list  best-, first-, next-fit Attempts to solve external fragmentation.
Computer Architecture & Operations I
CS2100 Computer Organisation
ECE 3430 – Intro to Microcomputer Systems
Computer Architecture & Operations I
Lecturer SOE Dan Garcia
C second, objective-c, Go up!
Lecturer SOE Dan Garcia
The University of Adelaide, School of Computer Science
3.
March 2006 Saeid Nooshabadi
March 2006 Saeid Nooshabadi
March, 2006 Saeid Nooshabadi
March 2006 Saeid Nooshabadi
9/27: Lecture Topics Memory Data transfer instructions
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

COMP3221 lec13-decision-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 13: Making Decisions in C/Assembly Language - I August, 2003 Saeid Nooshabadi

COMP3221 lec13-decision-I.2 Saeid Nooshabadi Review (#1/2) °Big idea in CS&E; compilation to translate from one level of abstraction to lower level Generally single HLL statement produces many assembly instructions Also hides address calculations (byte vs. word addressing) °Design of an Assembly Language like ARM shaped by 1) Desire to keep hardware simple: e.g., most operations have 3 operands 2) Smaller is faster: e.g., ARM has 16 registers

COMP3221 lec13-decision-I.3 Saeid Nooshabadi Review (#2/2) °ARM assembly language thus far: Instructions: add,sub,mov, orr,and,bic, eor, mul, ldr, str At most one assembly instruction per line Comments start with; to end of line Operands: registers r0 – r3  a1 – a4 (correspond to C functions arguments. Used for scratch pad too!) r4 – r10  v1 – v7 (correspond to function variables) Operands: memory Memory[0], Memory[4], Memory[8],,..., Memory[ ]

COMP3221 lec13-decision-I.4 Saeid Nooshabadi Baby Quiz °What are the three different ways to obtain a data operand you have seen? °Immediate - data is IN the instruction add r1, r1, #24 °Register Direct - data is IN a register the register number is in the instruction add r1, r1, r2 °Base plus offset - data is IN memory the register number is in the instruction the base address is in the register the offset is in the instruction/offset index register number in instruction ldr r1, [r2, #24] / ldr r1, [r2,r3] These are called Addressing Modes

COMP3221 lec13-decision-I.5 Saeid Nooshabadi Overview °C/Assembly if, goto, if-else °C /Assembly Loops: goto, while °Test for less Than, Greater Than, etc °C/Assembly case/switch statement °Conclusion

COMP3221 lec13-decision-I.6 Saeid Nooshabadi C Decisions (Control Flow): if statements °2 kinds of if statements in C if ( condition ) statement if ( condition ) statement1 else statement2 °Following code is same as 2nd if if ( condition ) goto L1; statement2; goto L2; L1: statement1; L2 : Not as elegant as if-else, but same meaning

COMP3221 lec13-decision-I.7 Saeid Nooshabadi ARM decision instructions (control flow) (#1/2) °Decision instruction in ARM: cmp register1, register2;compare register1 with ; register2 beq L1 ; branch to L1 if equal is “Branch if (registers are) equal” Same meaning as C: if (register1==register2) goto L1 °Complementary ARM decision instruction cmp register1, register2 bne L1 bne is “Branch if (registers are) not equal” Same meaning as C: if (register1!=register2) goto L1 °Called conditional branches

COMP3221 lec13-decision-I.8 Saeid Nooshabadi ARM decision instructions (control flow) (#2/2) °Decision instruction in ARM: cmp register1, #immediate ;compare register1 with ; immediate number beq L1 ; branch to L1 if equal is “Branch if (register and immediate are) equal” Same meaning as C: if (register1==#immediate) goto L1 °Complementary ARM decision instruction cmp register1, #immediate bne L1 bne is “Branch if (register and immediate are) not equal” Same meaning as C: if (register1!=#immediate) goto L1 °Called conditional branches

COMP3221 lec13-decision-I.9 Saeid Nooshabadi Compiling C if into ARM Assembly °Compile by hand if (i == j) f=g+h; else f=g-h; Mapping f : v1, g : v2, h : v3, i : v4, j : v5 °Start with branch: cmpv4, v5 beq L-true ; branch to L-True ; if i==j °Follow with false part sub v1,v2,v3 ; f=g-h exit i == j? f=g+hf=g-h (false) i != j (true) i == j

COMP3221 lec13-decision-I.10 Saeid Nooshabadi Compiling C if into ARM Assembly °Need instruction that always transfers control to skip over true part ARM has branch: b label ; goto “label” sub v1,v2,v3 ; f=g-h b L-exit °Next is true part L-true: add v1,v2,v3; f=g+h °Followed by exit branch label L-exit:

COMP3221 lec13-decision-I.11 Saeid Nooshabadi Compiling C if into ARM: Summary °Compile by hand if (i == j) f=g+h; else f=g-h; Mapping f : v1, g : v2, h : v3, i : v4, j : v5 cmp v4,v5 beq L-true ; branch i==j sub v1,v2,v3 ; (false) b L-exit ; go to Exit L-true: add v1,v2,v3 ;(true) L-exit : ° Note:Compiler supplies labels for branches not found in HLL code; often it flips the condition to branch to false part i == j? f=g+hf=g-h (false) i != j (true) i == j C ARMARM

COMP3221 lec13-decision-I.12 Saeid Nooshabadi Motoring with Microprocessors °Thanks to the magic of microprocessors and embedded systems, our cars are becoming safer, more efficient, and entertaining. °The average middle-class household includes over 40 embedded processors. About half are in the garage. Cars make a great vehicle for deploying embedded processors in huge numbers. These processors provide a ready source of power, ventilation, and mounting space and sell in terrific quantities. °How many embedded processors does your car have? °If you've got a late-model luxury sedan, two or three processors might be obvious in the GPS navigation system or the automatic distance control. Yet you'd still be off by a factor of 25 or 50. The current 7-Series BMW and S-class Mercedes boast about 100 processors apiece. A relatively low-profile Volvo still has 50 to 60 baby processors on board. Even a boring low-cost econobox has a few dozen different microprocessors in it. °Your transportation appliance probably has more chips than your Internet appliance. °New cars now frequently carry 200 pounds of electronics and more than a mile of wiring.

COMP3221 lec13-decision-I.13 Saeid Nooshabadi COMP3221 Reading Materials (Week #5) °Week #5: Steve Furber: ARM System On-Chip; 2nd Ed, Addison-Wesley, 2000, ISBN: We use chapters 3 and 5 °ARM Architecture Reference Manual –On CD ROM °A copy of the article by Cohen, D. “On holy wars and a plea for peace (data transmission).” Computer, vol.14, (no.10), Oct p.48-54, is place on the class website.

COMP3221 lec13-decision-I.14 Saeid Nooshabadi Loops in C/Assembly °Simple loop in C Loop:g = g + A[i]; i = i + j; if (i != h) goto Loop; °( g,h,i,j:v2,v3,v4,v5 ; base of A[]:v6 ): °1st fetch A[i] Loop: ldr a1,[v6, v4, lsl #2] ;(v6+v4*4)=addr A[i] ;a1=A[i]

COMP3221 lec13-decision-I.15 Saeid Nooshabadi Simple Loop (cont) °Add value of A[i] to g and then j to i g = g + a1 i = i + j; ° ( g,h,i,j:v2,v3,v4,v5 ): add v2,v2,a1; g = g+A[i] add v4,v4,v5; i = i + j The final instruction branches back to Loop if i != h : cmp v4,v3 bne Loop; goto Loop ; if i!=h

COMP3221 lec13-decision-I.16 Saeid Nooshabadi Loops in C/Assembly: Summary Loop:g = g + A[i]; i = i + j; if (i != h) goto Loop; °( g,h,i,j:v2,v3,v4,v5 ; base of A[]:v6 ): Loop: ldr a1,[v6, v4, lsl #2] ;(v6+v4*4)=addr A[i] ;a1=A[i] add v2,v2,a1 ; g = g+A[i] add v4,v4,v5 ; i = i + j cmp v4,v3 bneLoop ; goto Loop ; if i!=h C ARMARM

COMP3221 lec13-decision-I.17 Saeid Nooshabadi while in C/Assembly: °Although legal C, almost never write loops with if, goto : use while, or for loops °Syntax: while (condition) statement while (save[i] == k) i = i + j; °1st load save[i] into a scratch register ( i,j,k: v4,v5,v6 : base of save[]:v7 ): Loop: ldr a1,[v7,v4,lsl #2] ;v7+v4*4=addr of save[i] ;a1=save[i]

COMP3221 lec13-decision-I.18 Saeid Nooshabadi While in C/Assembly (cont) °Loop test: exit if save[i] != k ( i,j,k: v4,v5,v6 : base of save[]:v7 ) cmp a1,v6 bneExit;goto Exit ;if save[i]!=k °The next instruction adds j to i: add v4,v4,v5 ; i = i + j °End of loop branches back to the while test at top of loop. Add the Exit label after: b Loop ; goto Loop Exit:

COMP3221 lec13-decision-I.19 Saeid Nooshabadi While in C/Assembly: Summary while (save[i]==k) i = i + j; ( i,j,k: v4,v5,v6 : base of save[]:v7 ) Loop: ldr a1,[v7,v4,lsl #2] ;v7+v4*4=addr of save[i] ;a1=save[i] cmp a1,v6 bneExit;goto Exit ;if save[i]!=k add v4,v4,v5 ; i = i + j b Loop ; goto Loop Exit: C ARMARM

COMP3221 lec13-decision-I.20 Saeid Nooshabadi Beyond equality tests in ARM Assembly (#1/2) °So far ==, !=, What about ? cmp register1, register2 blt L1 is “Branch if (register1 <register2) Same meaning as C: if (register1<register2) go to L1 °Complementary ARM decision instruction cmp register1, register2 bge L1 bge is “Branch if (register1 >= register2) ” Same meaning as C: if (register1>=register2) go to L1

COMP3221 lec13-decision-I.21 Saeid Nooshabadi Beyond equality tests in ARM Assembly (#2/2) °Also cmp register1, #immediate blt L1 is “Branch if (register1 <#immediate) Same meaning as C: if (register1<immediate) go to L1 °Complementary ARM decision instruction cmp register1, #immediate bge L1 bge is “Branch if (register1 >= #immediate) ” Same meaning as C: if (register1>=immediate) go to L1

COMP3221 lec13-decision-I.22 Saeid Nooshabadi If less_than in C/Assembly if (g < h) {... } cmp v1,v2 ; v1<v2 (g<h) blt Less ; if (g = h) Less:... noLess: Alternative Code cmp v1,v2 ; v1 = h)... ; if (g < h) noLess: C ARMARM

COMP3221 lec13-decision-I.23 Saeid Nooshabadi Some Branch Conditions °b Unconditional °bal Branch Always °beq Branch Equal °bne Branch Not Equal °blt Branch Less Than °ble Branch Less Than or Equal °bgt Branch Greater Than °bge Branch Greater Than or Equal °Full Table Page 64 Steve Furber: ARM System On- Chip; 2nd Ed, Addison-Wesley, 2000, ISBN:

COMP3221 lec13-decision-I.24 Saeid Nooshabadi What about unsigned numbers? °Conditional branch instructions blt, ble, bgt, etc, assume signed operands (defined as int in C). The equivalent instructions for unsigned operands (defined as unsigned in C). are: °blo Branch Lower (unsigned) °bls Branch Less or Same (unsigned) °bhi Branch Higher (unsigned) °bhs Branch Higher or Same (Unsigned) °v1 = FFFF FFFA hex, v2 = 0000 FFFA hex °What is result of cmpv1, v2cmpv1, v2 bgtL1 bhiL1

COMP3221 lec13-decision-I.25 Saeid Nooshabadi Signed VS Unsigned Comparison °v1 = FFFF FFFA hex, v2 = 0000 FFFA hex °v1 < v2 (signed interpretation) °v1 > v2 (unsigned interpretation) °What is result of cmpv1, v2cmpv1, v2 bgtL1bhiL1 … … L1: Branch NOT taken Branch Taken

COMP3221 lec13-decision-I.26 Saeid Nooshabadi Branches: PC-relative addressing °Recall register r15 in the machine also called PC; °points to the currently executing instruction °Most instruction add 4 to it. (pc increments by 4 after execution of most instructions) °Branch changes it to a specific value °Branch adds to it 24-bit signed value (contained in the instruction) Shifted left by 2 bits °Labels => addresses memory 0: FFF... registers r14 r0 r15 = pc beq address b address –32MB +32MB 24 bits

COMP3221 lec13-decision-I.27 Saeid Nooshabadi C case/switch statement °Choose among four alternatives depending on whether k has the value 0, 1, 2, or 3 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*/ }

COMP3221 lec13-decision-I.28 Saeid Nooshabadi Case/switch via chained if-else, C °Could be done like chain of if-else if(k==0) f=i+j; else if(k==1) f=g+h; else if(k==2) f=g–h; else if(k==3) f=i–j;

COMP3221 lec13-decision-I.29 Saeid Nooshabadi Case/switch via chained if-else, C/Asm. °Could be done like chain of if-else if(k==0) f=i+j; else if(k==1) f=g+h; else if(k==2) f=g–h; else if(k==3) f=i–j; ( f,i,j,g,h,k:v1,v2,v3,v4,v5,v6) cmp v6,#0 bne L1 ; branch k!=0 add v1,v2,v3 ; k=0 so f=i+j b Exit ; end of case L1:cmp v6,#1 bne L2 ; branch k!=1 add v1,v4,v5 ; k=1 so f=g+h b Exit ; end of case L2:cmp v6,#2 bne L3 ; branch k!=2 sub v1,v4,v5 ; k=2 so f=g-h b Exit ; end of case L3:cmp v6,#3 bne Exit ; branch k!=2 sub v1,v2,v3 ; k=3 so f=i-j Exit: C ARMARM

COMP3221 lec13-decision-I.30 Saeid Nooshabadi Case/Switch via Jump Address Table °Notice that last case must wait for n-1 tests before executing, making it slow °Alternative tries to go to all cases equally fast: jump address table Idea: encode alternatives as a table of addresses of the cases -Table an array of words with addresses corresponding to case labels Program indexes into table and jumps °ARM instruction “ldr pc, [ ]” unconditionally branches to address L1 (Changes PC to address of L1)

COMP3221 lec13-decision-I.31 Saeid Nooshabadi Idea for Case using Jump Table check within range get address of target clause from target array jump to target address Default clause end_of_case : code for clause jump to end_of_case Address Jump Table

COMP3221 lec13-decision-I.32 Saeid Nooshabadi Case/Switch via Jump Address Table (#1/3) °Use k to index a jump address table, and then jump via the value loaded °1st test that k matches 1 of cases (0<=k<=3); if not, the code exits ( k:v6, v7: Base address of JumpTable[k] ) cmp v6, #0;Test if k 3 bgt Exit ;if k>3,goto Exit

COMP3221 lec13-decision-I.33 Saeid Nooshabadi Case/Switch via Jump Address Table (#2/3) °Assume 4 sequential words (4 bytes) in memory, with base address in v7, have addresses corresponding to labels L0, L1, L2, L3. °Now use (4* k ) ( k:v6 ) to index table of words and load the clause address from the table (Address of labels L0, L1, L2, L3 ) to register pc. ldr pc, [v7, v6,lsl #2] ;JumpTable[k]= v7 + (v6*4) (Register Indexed Addressing) PC will contain the address of the clause and execution starts from there. L3 L0 L1 L2 v7 V6*4

COMP3221 lec13-decision-I.34 Saeid Nooshabadi Case/Switch via Jump Address Table (#3/3) °Cases jumped to by ldr pc, [v7, v6,lsl #2] : L0: add v1,v2,v3 ; k=1 so f=i+j b Exit ; end of case L1: add v1,v4,v5 ; k=1 so f=g+h b Exit ; end of case L2:sub v1,v4,v5 ; k=2 so f=g-h b Exit ; end of case L3: sub v1,v2,v3 ; k=3 so f=i-j Exit:

COMP3221 lec13-decision-I.35 Saeid Nooshabadi Jump Address Table: Summary ( k,f,i,j,g,h Base address of JumpTable[k]) :v6,v1,v2,v3,v4,v5,v7) cmp v6, #0;Test if k 3 bgt Exit ;if k>3,goto Exit ldr pc, [v7, v6,lsl #2] ;JumpTable[k]= v7 + (v6*4) L0: add v1,v2,v3 ; k=1 so f=i+j b Exit ; end of case L1: add v1,v4,v5 ; k=1 so f=g+h b Exit ; end of case L2:sub v1,v4,v5 ; k=2 so f=g-h b Exit ; end of case L3: sub v1,v2,v3 ; k=3 so f=i-j Exit: More example on Jump Tables on CD-ROM

COMP3221 lec13-decision-I.36 Saeid Nooshabadi If there is time, do it yourself: °Compile this C code into ARM: sum = 0; for (i=0;i<10;i=i+1) sum = sum + A[i]; sum : v1, i : v2, base address of A : v3

COMP3221 lec13-decision-I.37 Saeid Nooshabadi (If time allows) Do it yourself: sum = 0; for (i=0;i<10;i=i+1) sum = sum + A[i]; sum : v1, i : v2, base address of A : v3 C ARMARM mov v1, #0 mov v2, #0 Loop: ldr a1,[v3,v2,lsl #2] ; a1=A[i] add v1, v1, a1 ; sum = sum+A [i] add v2, v2, #1 ; increment i cmp v2, #10 ; Check(i<10) bne Loop ; goto loop

COMP3221 lec13-decision-I.38 Saeid Nooshabadi “And in Conclusion …” (#1/2) °HLL decisions (if, case) and loops (while, for) use same assembly instructions Comparison: cmp in ARM Conditional branches: beq, bne in ARM Unconditional Jumps: b, ldr pc,____, mov pc, ____ in ARM Case/Switch: either chained if-else or jump table + ldr pc, ____

COMP3221 lec13-decision-I.39 Saeid Nooshabadi “And in Conclusion…” (#1/2) °New Instructions: cmp beq bne bgt bge blt ble bhi bhs blo bls