SlideShare a Scribd company logo
1 of 73
1
Digital Systems and Microprocessor Design
(H7068)
Daniel Roggen
d.roggen@sussex.ac.uk
9.2. Jumps and
loops
2
Content
• Loops with unconditional jumps
• Conditional jumps
• Conditional loops
• C-style loops to assembler
3
Loop with unconditional jump
• Unconditional jumps: changes the value of PC to
destination
– jmp dst
• To do forever a task
– Polling-based event loop
– sensing-actuation loop
• Example:
– Read sensors
– Compute motor speed
– Set motor speed
4
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
-> 00 mov ra,3h 0 0 0
0
02 sub ra,1h
04 jmp 2h
06 ???
5
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h 3 0 0
0
-> 02 sub ra,1h
04 jmp 2h
06 ???
6
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h 2 0 0
0
02 sub ra,1h
-> 04 jmp 2h
06 ???
7
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h 2 0 0
0
-> 02 sub ra,1h
04 jmp 2h
06 ???
8
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h 1 0 0
0
02 sub ra,1h
-> 04 jmp 2h
06 ???
9
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h 1 0 0
0
-> 02 sub ra,1h
04 jmp 2h
06 ???
10
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h 0 0 0
0
02 sub ra,1h
-> 04 jmp 2h
06 ???
11
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h 0 0 0
0
-> 02 sub ra,1h
04 jmp 2h
06 ???
12
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h FF 0 0
0
02 sub ra,1h
-> 04 jmp 2h
06 ???
13
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h FF 0 0
0
-> 02 sub ra,1h
04 jmp 2h
06 ???
14
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h FE 0 0
0
02 sub ra,1h
-> 04 jmp 2h
06 ???
15
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h FE 0 0
0
-> 02 sub ra,1h
04 jmp 2h
06 ???
16
Loop with unconditional jump
PC Adr Instr RA RB RC
RD
00 mov ra,3h FE 0 0
0
-> 02 sub ra,1h
04 jmp 2h
06 ??? This line is never executed!
17
Conditional jumps
• Conditional jumps: changes the value of PC if a condition
is met.
• Condition is tested by checking the flags (carry, zero).
• Flags are set by a prior comparison
• JA: jump if above
– Jumps if Zero=0 and Carry=0
• JB: jump if below
– Jumps if Zero=0 and Carry=1
• JE: jump if equal
– Jumps if Zero=1
• And the opposite: JNA, JNB, JNE (not above, not below,
not equal)
18
Loop with conditional jump (jne)
• Loop with variable from startvalue to 0 (inclusive)
19
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
-> 00 mov ra,3h
02 sub ra,1h
04 cmp ra,0h
06 jne 02
08 ???
20
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0
00 mov ra,3h
-> 02 sub ra,1h
04 cmp ra,0h
06 jne 02
08 ???
21
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
02 sub ra,1h
-> 04 cmp ra,0h
06 jne 02
08 ???
22
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
02 sub ra,1h
04 cmp ra,0h
-> 06 jne 02
08 ???
23
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
-> 02 sub ra,1h
04 cmp ra,0h
06 jne 02
08 ???
24
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
02 sub ra,1h
-> 04 cmp ra,0h
06 jne 02
08 ???
25
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
02 sub ra,1h
04 cmp ra,0h
-> 06 jne 02
08 ???
26
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
-> 02 sub ra,1h
04 cmp ra,0h
06 jne 02
08 ???
27
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
00 mov ra,3h
02 sub ra,1h
-> 04 cmp ra,0h
06 jne 02
08 ???
28
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0 Z
00 mov ra,3h
02 sub ra,1h
04 cmp ra,0h
-> 06 jne 02
08 ???
29
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0 Z
00 mov ra,3h
02 sub ra,1h
04 cmp ra,0h
06 jne 02
-> 08 ???
30
Loop with conditional jump (jne)
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0 Z
00 mov ra,3h
02 sub ra,1h
04 cmp ra,0h
06 jne 02
-> 08 ???
Program continues execution
31
C to assembler loops
• In C the syntax for a for loop is:
• for(<initialization>;<condition>; <update>) {code}
• Example: for(i=3; i != 0; i--) {....}
– i will take the value: 3, 2, 1.
• Can be easily translated to assembler
32
C to assembler loops: alternative 1
• for(<initialization>;<condition>; <update>) {code}
initialization
test:
test condition
if condition then jump to loopcode
jump to endofloop
loopcode:
code
update
jump to test
endofloop:
rest of program
unconditional
conditional
33
C to assembler loops: alternative 2
• for(<initialization>;<condition>; <update>) {code}
initialization
test:
test condition
if not condition then jump to endofloop
code
update
jump to test
endofloop:
rest of program
unconditional
conditional
This requires to negate the condition!
Processors usually provide conditional jumps if condition (je,ja,jb)
and conditional jumps if not condition (jne,jna,jnb)
34
C to assembler loops
• With alternative 1 the conditional jump is to a nearby
address (instruction skip)
• Alternative 2 leads to more compact code
• Some processors have "relative jumps" that allow to
change PC by an offset
– On Intel/AMD x86 the "short relative jump" allows to offset PC by
up to -128 to +127 bytes
• What happens on x86 if the loop code is longer than 127
bytes?
– Alternative 2 cannot be used with short relative jump!
– Alternative 1 must be used
Programming influenced by processor architecture!
That is why higher level languages (e.g. C) and compilers are
used: they can optimize the code
Programming influenced by processor architecture!
Higher level languages (e.g. C) and compilers allow to select the
right assembler construct to optimize the code
35
C to assembler loops
• for(i=3; i != 0; i--) {....}
• Alternative 2
36
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
-> 00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
37
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0
00 mov ra,3h
-> 02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
38
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0
00 mov ra,3h
02 cmp ra,0h
-> 04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
39
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
-> 06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
40
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
-> 0A sub ra,1h
0C jmp 02
0E ???
41
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
-> 0C jmp 02
0E ???
42
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
-> 02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
43
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
02 cmp ra,0h
-> 04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
44
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
-> 06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
45
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
-> 0A sub ra,1h
0C jmp 02
0E ???
46
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
-> 0C jmp 02
0E ???
47
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
-> 02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
48
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
02 cmp ra,0h
-> 04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
49
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
-> 06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
50
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
-> 0A sub ra,1h
0C jmp 02
0E ???
51
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
-> 0C jmp 02
0E ???
52
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
00 mov ra,3h
-> 02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
53
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0 Z
00 mov ra,3h
02 cmp ra,0h
-> 04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
0E ???
54
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0 Z
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
-> 0E ???
55
for(i=3; i != 0; i--) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0 Z
00 mov ra,3h
02 cmp ra,0h
04 je 0eh
06 ...
08 ...
0A sub ra,1h
0C jmp 02
-> 0E ???
Program continues execution
56
C to assembler loops
• for(i=3; i != 0; i--) {....}
• Alternative 1
-> 00 mov ra,3h
02 cmp ra,0h
04 jne 08h
06 jmp 0E
08 ...
0A sub ra,1h
0C jmp 02
0E ???
57
C to assembler loops
• for(i=0; i <3; i++) {....}
– i takes the values: 0, 1, 2
• Alternative 1
58
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
-> 00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
59
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
00 mov ra,0h
-> 02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
60
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
C
00 mov ra,0h
02 cmp ra,3h
-> 04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
61
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
C
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
-> 08 ...
0A add ra,1h
0C jmp 02
0E ???
62
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
0 0 0 0
C
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
-> 0A add ra,1h
0C jmp 02
0E ???
63
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
C
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
-> 0C jmp 02
0E ???
64
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
C
00 mov ra,0h
-> 02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
65
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
1 0 0 0
C
00 mov ra,0h
02 cmp ra,3h
-> 04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
fast forward
som
e
steps...
66
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
2 0 0 0
C
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
-> 0A add ra,1h
0C jmp 02
0E ???
67
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0
C
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
-> 0C jmp 02
0E ???
68
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0
C
00 mov ra,0h
-> 02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
69
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0 Z
00 mov ra,0h
02 cmp ra,3h
-> 04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
70
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0 Z
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
-> 06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
0E ???
71
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0 Z
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
-> 0E ???
72
for(i=0; i <3; i++) {....}
PC Adr Instr RA RB RC RD
FLAGS
3 0 0 0 Z
00 mov ra,0h
02 cmp ra,3h
04 jb 08h
06 jmp 0E
08 ...
0A add ra,1h
0C jmp 02
-> 0E ??? Program continues execution
73
Summary
• Basic loop constructs can be realized in assembler
• Pay attention to the desired range of values of the
variables and where the test is placed!
• The "C to assembler" examples generalize to more
complex tests!
– for(i=0; (i<100) && (obstacle1==0); i++) {....}
– Use boolean logic to combine multiple simple tests together
– Or test individual parts and have several conditional jumps
• Knowledge sufficient to complete the coursework
assignment involving programming

More Related Content

What's hot

Weapons grade React by Ken Wheeler
Weapons grade React by Ken Wheeler Weapons grade React by Ken Wheeler
Weapons grade React by Ken Wheeler React London 2017
 
ARM Polyglot Shellcode - HITB2019AMS
ARM Polyglot Shellcode - HITB2019AMSARM Polyglot Shellcode - HITB2019AMS
ARM Polyglot Shellcode - HITB2019AMSSaumil Shah
 
Standard cells library design
Standard cells library designStandard cells library design
Standard cells library designBharat Biyani
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersCorrado Santoro
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Michael Barker
 
Using ARM Dev.Board in physical experimental instruments
Using ARM Dev.Board in physical experimental instrumentsUsing ARM Dev.Board in physical experimental instruments
Using ARM Dev.Board in physical experimental instrumentsa_n0v
 
Schrödinger's ARM Assembly
Schrödinger's ARM AssemblySchrödinger's ARM Assembly
Schrödinger's ARM AssemblySaumil Shah
 

What's hot (10)

Weapons grade React by Ken Wheeler
Weapons grade React by Ken Wheeler Weapons grade React by Ken Wheeler
Weapons grade React by Ken Wheeler
 
ARM Polyglot Shellcode - HITB2019AMS
ARM Polyglot Shellcode - HITB2019AMSARM Polyglot Shellcode - HITB2019AMS
ARM Polyglot Shellcode - HITB2019AMS
 
Standard cells library design
Standard cells library designStandard cells library design
Standard cells library design
 
Hy 523
Hy 523Hy 523
Hy 523
 
Using Timers in PIC18F Microcontrollers
Using Timers in PIC18F MicrocontrollersUsing Timers in PIC18F Microcontrollers
Using Timers in PIC18F Microcontrollers
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!
 
STANDARD CELL LIBRARY DESIGN
STANDARD CELL LIBRARY DESIGNSTANDARD CELL LIBRARY DESIGN
STANDARD CELL LIBRARY DESIGN
 
Using ARM Dev.Board in physical experimental instruments
Using ARM Dev.Board in physical experimental instrumentsUsing ARM Dev.Board in physical experimental instruments
Using ARM Dev.Board in physical experimental instruments
 
Schrödinger's ARM Assembly
Schrödinger's ARM AssemblySchrödinger's ARM Assembly
Schrödinger's ARM Assembly
 
Using Timer1 and CCP
Using Timer1 and CCPUsing Timer1 and CCP
Using Timer1 and CCP
 

Similar to Digital Systems and Microprocessor Design Loops

Reverse engineering of binary programs for custom virtual machines
Reverse engineering of binary programs for custom virtual machinesReverse engineering of binary programs for custom virtual machines
Reverse engineering of binary programs for custom virtual machinesSmartDec
 
Winter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate TrainingWinter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate TrainingTechnogroovy
 
ARM Architecture Instruction Set
ARM Architecture Instruction SetARM Architecture Instruction Set
ARM Architecture Instruction SetDwight Sabio
 
Cache_Model for basic understnading of axi
Cache_Model for basic understnading of axiCache_Model for basic understnading of axi
Cache_Model for basic understnading of axiSandipSolanki10
 
zen and the art of SQL optimization
zen and the art of SQL optimizationzen and the art of SQL optimization
zen and the art of SQL optimizationKaren Morton
 
Microstepping of stepper Motor
Microstepping of stepper MotorMicrostepping of stepper Motor
Microstepping of stepper MotorANUP PALARAPWAR
 
Basic Tutorial for Robotic Arm
Basic Tutorial for Robotic ArmBasic Tutorial for Robotic Arm
Basic Tutorial for Robotic ArmYu Wei Chen
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itSergey Platonov
 
Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051logesh waran
 
Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimonSisimon Soman
 
The_ERICSSON_commands_listed_below_are_f (1) (1).pdf
The_ERICSSON_commands_listed_below_are_f (1) (1).pdfThe_ERICSSON_commands_listed_below_are_f (1) (1).pdf
The_ERICSSON_commands_listed_below_are_f (1) (1).pdfssuser340a0c
 
Kernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesKernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesAnne Nicolas
 
Buy Embedded Systems Projects Online,Buy B tech Projects Online
Buy Embedded Systems Projects Online,Buy B tech Projects OnlineBuy Embedded Systems Projects Online,Buy B tech Projects Online
Buy Embedded Systems Projects Online,Buy B tech Projects OnlineTechnogroovy
 
PPT FINAL (1)-1 (1).ppt
PPT FINAL (1)-1 (1).pptPPT FINAL (1)-1 (1).ppt
PPT FINAL (1)-1 (1).ppttariqqureshi33
 
Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)Ontico
 

Similar to Digital Systems and Microprocessor Design Loops (20)

Reverse engineering of binary programs for custom virtual machines
Reverse engineering of binary programs for custom virtual machinesReverse engineering of binary programs for custom virtual machines
Reverse engineering of binary programs for custom virtual machines
 
OptimizingARM
OptimizingARMOptimizingARM
OptimizingARM
 
Winter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate TrainingWinter training,Readymade Projects,Buy Projects,Corporate Training
Winter training,Readymade Projects,Buy Projects,Corporate Training
 
ARM Architecture Instruction Set
ARM Architecture Instruction SetARM Architecture Instruction Set
ARM Architecture Instruction Set
 
Instruction types
Instruction typesInstruction types
Instruction types
 
Cache_Model for basic understnading of axi
Cache_Model for basic understnading of axiCache_Model for basic understnading of axi
Cache_Model for basic understnading of axi
 
zen and the art of SQL optimization
zen and the art of SQL optimizationzen and the art of SQL optimization
zen and the art of SQL optimization
 
Microstepping of stepper Motor
Microstepping of stepper MotorMicrostepping of stepper Motor
Microstepping of stepper Motor
 
Basic Tutorial for Robotic Arm
Basic Tutorial for Robotic ArmBasic Tutorial for Robotic Arm
Basic Tutorial for Robotic Arm
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
 
Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051Addressing mode and instruction set using 8051
Addressing mode and instruction set using 8051
 
Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimon
 
P1111214158
P1111214158P1111214158
P1111214158
 
The_ERICSSON_commands_listed_below_are_f (1) (1).pdf
The_ERICSSON_commands_listed_below_are_f (1) (1).pdfThe_ERICSSON_commands_listed_below_are_f (1) (1).pdf
The_ERICSSON_commands_listed_below_are_f (1) (1).pdf
 
Kernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesKernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering Oopsies
 
Buy Embedded Systems Projects Online,Buy B tech Projects Online
Buy Embedded Systems Projects Online,Buy B tech Projects OnlineBuy Embedded Systems Projects Online,Buy B tech Projects Online
Buy Embedded Systems Projects Online,Buy B tech Projects Online
 
PPT FINAL (1)-1 (1).ppt
PPT FINAL (1)-1 (1).pptPPT FINAL (1)-1 (1).ppt
PPT FINAL (1)-1 (1).ppt
 
Project ppt
Project pptProject ppt
Project ppt
 
Pclr syllabus 1 month
Pclr syllabus  1 monthPclr syllabus  1 month
Pclr syllabus 1 month
 
Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)
 

More from Daniel Roggen

Embedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour ScienceEmbedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour ScienceDaniel Roggen
 
W9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory AccessW9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory AccessDaniel Roggen
 
W8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational ProcessorW8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational ProcessorDaniel Roggen
 
W8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational ProcessorW8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational ProcessorDaniel Roggen
 
Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?Daniel Roggen
 
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...Daniel Roggen
 
Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)Daniel Roggen
 
Wearable Computing - Part II: Sensors
Wearable Computing - Part II: SensorsWearable Computing - Part II: Sensors
Wearable Computing - Part II: SensorsDaniel Roggen
 
Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?Daniel Roggen
 

More from Daniel Roggen (12)

Embedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour ScienceEmbedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour Science
 
W10: Laboratory
W10: LaboratoryW10: Laboratory
W10: Laboratory
 
W9: Laboratory 2
W9: Laboratory 2W9: Laboratory 2
W9: Laboratory 2
 
W9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory AccessW9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory Access
 
W8: Laboratory 1
W8: Laboratory 1W8: Laboratory 1
W8: Laboratory 1
 
W8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational ProcessorW8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational Processor
 
W8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational ProcessorW8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational Processor
 
Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?
 
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
 
Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)
 
Wearable Computing - Part II: Sensors
Wearable Computing - Part II: SensorsWearable Computing - Part II: Sensors
Wearable Computing - Part II: Sensors
 
Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?
 

Recently uploaded

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 

Recently uploaded (20)

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 

Digital Systems and Microprocessor Design Loops

  • 1. 1 Digital Systems and Microprocessor Design (H7068) Daniel Roggen d.roggen@sussex.ac.uk 9.2. Jumps and loops
  • 2. 2 Content • Loops with unconditional jumps • Conditional jumps • Conditional loops • C-style loops to assembler
  • 3. 3 Loop with unconditional jump • Unconditional jumps: changes the value of PC to destination – jmp dst • To do forever a task – Polling-based event loop – sensing-actuation loop • Example: – Read sensors – Compute motor speed – Set motor speed
  • 4. 4 Loop with unconditional jump PC Adr Instr RA RB RC RD -> 00 mov ra,3h 0 0 0 0 02 sub ra,1h 04 jmp 2h 06 ???
  • 5. 5 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h 3 0 0 0 -> 02 sub ra,1h 04 jmp 2h 06 ???
  • 6. 6 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h 2 0 0 0 02 sub ra,1h -> 04 jmp 2h 06 ???
  • 7. 7 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h 2 0 0 0 -> 02 sub ra,1h 04 jmp 2h 06 ???
  • 8. 8 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h 1 0 0 0 02 sub ra,1h -> 04 jmp 2h 06 ???
  • 9. 9 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h 1 0 0 0 -> 02 sub ra,1h 04 jmp 2h 06 ???
  • 10. 10 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h 0 0 0 0 02 sub ra,1h -> 04 jmp 2h 06 ???
  • 11. 11 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h 0 0 0 0 -> 02 sub ra,1h 04 jmp 2h 06 ???
  • 12. 12 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h FF 0 0 0 02 sub ra,1h -> 04 jmp 2h 06 ???
  • 13. 13 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h FF 0 0 0 -> 02 sub ra,1h 04 jmp 2h 06 ???
  • 14. 14 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h FE 0 0 0 02 sub ra,1h -> 04 jmp 2h 06 ???
  • 15. 15 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h FE 0 0 0 -> 02 sub ra,1h 04 jmp 2h 06 ???
  • 16. 16 Loop with unconditional jump PC Adr Instr RA RB RC RD 00 mov ra,3h FE 0 0 0 -> 02 sub ra,1h 04 jmp 2h 06 ??? This line is never executed!
  • 17. 17 Conditional jumps • Conditional jumps: changes the value of PC if a condition is met. • Condition is tested by checking the flags (carry, zero). • Flags are set by a prior comparison • JA: jump if above – Jumps if Zero=0 and Carry=0 • JB: jump if below – Jumps if Zero=0 and Carry=1 • JE: jump if equal – Jumps if Zero=1 • And the opposite: JNA, JNB, JNE (not above, not below, not equal)
  • 18. 18 Loop with conditional jump (jne) • Loop with variable from startvalue to 0 (inclusive)
  • 19. 19 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 -> 00 mov ra,3h 02 sub ra,1h 04 cmp ra,0h 06 jne 02 08 ???
  • 20. 20 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 00 mov ra,3h -> 02 sub ra,1h 04 cmp ra,0h 06 jne 02 08 ???
  • 21. 21 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h 02 sub ra,1h -> 04 cmp ra,0h 06 jne 02 08 ???
  • 22. 22 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h 02 sub ra,1h 04 cmp ra,0h -> 06 jne 02 08 ???
  • 23. 23 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h -> 02 sub ra,1h 04 cmp ra,0h 06 jne 02 08 ???
  • 24. 24 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h 02 sub ra,1h -> 04 cmp ra,0h 06 jne 02 08 ???
  • 25. 25 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h 02 sub ra,1h 04 cmp ra,0h -> 06 jne 02 08 ???
  • 26. 26 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h -> 02 sub ra,1h 04 cmp ra,0h 06 jne 02 08 ???
  • 27. 27 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 00 mov ra,3h 02 sub ra,1h -> 04 cmp ra,0h 06 jne 02 08 ???
  • 28. 28 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 Z 00 mov ra,3h 02 sub ra,1h 04 cmp ra,0h -> 06 jne 02 08 ???
  • 29. 29 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 Z 00 mov ra,3h 02 sub ra,1h 04 cmp ra,0h 06 jne 02 -> 08 ???
  • 30. 30 Loop with conditional jump (jne) PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 Z 00 mov ra,3h 02 sub ra,1h 04 cmp ra,0h 06 jne 02 -> 08 ??? Program continues execution
  • 31. 31 C to assembler loops • In C the syntax for a for loop is: • for(<initialization>;<condition>; <update>) {code} • Example: for(i=3; i != 0; i--) {....} – i will take the value: 3, 2, 1. • Can be easily translated to assembler
  • 32. 32 C to assembler loops: alternative 1 • for(<initialization>;<condition>; <update>) {code} initialization test: test condition if condition then jump to loopcode jump to endofloop loopcode: code update jump to test endofloop: rest of program unconditional conditional
  • 33. 33 C to assembler loops: alternative 2 • for(<initialization>;<condition>; <update>) {code} initialization test: test condition if not condition then jump to endofloop code update jump to test endofloop: rest of program unconditional conditional This requires to negate the condition! Processors usually provide conditional jumps if condition (je,ja,jb) and conditional jumps if not condition (jne,jna,jnb)
  • 34. 34 C to assembler loops • With alternative 1 the conditional jump is to a nearby address (instruction skip) • Alternative 2 leads to more compact code • Some processors have "relative jumps" that allow to change PC by an offset – On Intel/AMD x86 the "short relative jump" allows to offset PC by up to -128 to +127 bytes • What happens on x86 if the loop code is longer than 127 bytes? – Alternative 2 cannot be used with short relative jump! – Alternative 1 must be used Programming influenced by processor architecture! That is why higher level languages (e.g. C) and compilers are used: they can optimize the code Programming influenced by processor architecture! Higher level languages (e.g. C) and compilers allow to select the right assembler construct to optimize the code
  • 35. 35 C to assembler loops • for(i=3; i != 0; i--) {....} • Alternative 2
  • 36. 36 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 -> 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 37. 37 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 00 mov ra,3h -> 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 38. 38 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 00 mov ra,3h 02 cmp ra,0h -> 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 39. 39 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh -> 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 40. 40 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... -> 0A sub ra,1h 0C jmp 02 0E ???
  • 41. 41 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h -> 0C jmp 02 0E ???
  • 42. 42 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h -> 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 43. 43 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h 02 cmp ra,0h -> 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 44. 44 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh -> 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 45. 45 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... -> 0A sub ra,1h 0C jmp 02 0E ???
  • 46. 46 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h -> 0C jmp 02 0E ???
  • 47. 47 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h -> 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 48. 48 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h 02 cmp ra,0h -> 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 49. 49 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh -> 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 50. 50 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... -> 0A sub ra,1h 0C jmp 02 0E ???
  • 51. 51 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h -> 0C jmp 02 0E ???
  • 52. 52 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 00 mov ra,3h -> 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 53. 53 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 Z 00 mov ra,3h 02 cmp ra,0h -> 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 54. 54 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 Z 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 -> 0E ???
  • 55. 55 for(i=3; i != 0; i--) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 Z 00 mov ra,3h 02 cmp ra,0h 04 je 0eh 06 ... 08 ... 0A sub ra,1h 0C jmp 02 -> 0E ??? Program continues execution
  • 56. 56 C to assembler loops • for(i=3; i != 0; i--) {....} • Alternative 1 -> 00 mov ra,3h 02 cmp ra,0h 04 jne 08h 06 jmp 0E 08 ... 0A sub ra,1h 0C jmp 02 0E ???
  • 57. 57 C to assembler loops • for(i=0; i <3; i++) {....} – i takes the values: 0, 1, 2 • Alternative 1
  • 58. 58 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 -> 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 59. 59 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 00 mov ra,0h -> 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 60. 60 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 C 00 mov ra,0h 02 cmp ra,3h -> 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 61. 61 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 C 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E -> 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 62. 62 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 0 0 0 0 C 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... -> 0A add ra,1h 0C jmp 02 0E ???
  • 63. 63 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 C 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h -> 0C jmp 02 0E ???
  • 64. 64 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 C 00 mov ra,0h -> 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 65. 65 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 1 0 0 0 C 00 mov ra,0h 02 cmp ra,3h -> 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ??? fast forward som e steps...
  • 66. 66 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 2 0 0 0 C 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... -> 0A add ra,1h 0C jmp 02 0E ???
  • 67. 67 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 C 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h -> 0C jmp 02 0E ???
  • 68. 68 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 C 00 mov ra,0h -> 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 69. 69 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 Z 00 mov ra,0h 02 cmp ra,3h -> 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 70. 70 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 Z 00 mov ra,0h 02 cmp ra,3h 04 jb 08h -> 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 0E ???
  • 71. 71 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 Z 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 -> 0E ???
  • 72. 72 for(i=0; i <3; i++) {....} PC Adr Instr RA RB RC RD FLAGS 3 0 0 0 Z 00 mov ra,0h 02 cmp ra,3h 04 jb 08h 06 jmp 0E 08 ... 0A add ra,1h 0C jmp 02 -> 0E ??? Program continues execution
  • 73. 73 Summary • Basic loop constructs can be realized in assembler • Pay attention to the desired range of values of the variables and where the test is placed! • The "C to assembler" examples generalize to more complex tests! – for(i=0; (i<100) && (obstacle1==0); i++) {....} – Use boolean logic to combine multiple simple tests together – Or test individual parts and have several conditional jumps • Knowledge sufficient to complete the coursework assignment involving programming