W9_2: Jumps and loops

danielroggen 94 views 73 slides Dec 05, 2017
Slide 1
Slide 1 of 73
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73

About This Presentation

In this unit we explain how jumps and loops can be implemented in assembler on the UoS Educational Processor.
We also explain how for loops constructs expressed in C translate naturally to equivalent assembler constructs.

Unit duration: 50mn.
License: LGPL 2.1


Slide Content

1
Digital Systems and Microprocessor Design
(H7068)
Daniel Roggen
[email protected]
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
PCAdrInstr RA RB RC
RD
->00 movra,3h 0 0 0
0
02 subra,1h
04 jmp2h
06 ???

5
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h 3 0 0
0
-> 02 subra,1h
04 jmp2h
06 ???

6
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h 2 0 0
0
02 subra,1h
-> 04 jmp2h
06 ???

7
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h 2 0 0
0
-> 02 subra,1h
04 jmp2h
06 ???

8
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h 1 0 0
0
02 subra,1h
-> 04 jmp2h
06 ???

9
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h 1 0 0
0
-> 02 subra,1h
04 jmp2h
06 ???

10
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h 0 0 0
0
02 subra,1h
-> 04 jmp2h
06 ???

11
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h 0 0 0
0
-> 02 subra,1h
04 jmp2h
06 ???

12
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h FF 0 0
0
02 subra,1h
-> 04 jmp2h
06 ???

13
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h FF 0 0
0
-> 02 subra,1h
04 jmp2h
06 ???

14
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h FE 0 0
0
02 subra,1h
-> 04 jmp2h
06 ???

15
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h FE 0 0
0
-> 02 subra,1h
04 jmp2h
06 ???

16
Loop with unconditional jump
PCAdrInstr RA RB RC
RD
00 movra,3h FE 0 0
0
-> 02 subra,1h
04 jmp2h
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)
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
->00 movra,3h
02 subra,1h
04 cmpra,0h
06 jne02
08 ???

20
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0
00 movra,3h
-> 02 subra,1h
04 cmpra,0h
06 jne02
08 ???

21
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
02 subra,1h
-> 04 cmpra,0h
06 jne02
08 ???

22
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
02 subra,1h
04 cmpra,0h
-> 06 jne02
08 ???

23
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
-> 02 subra,1h
04 cmpra,0h
06 jne02
08 ???

24
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
02 subra,1h
-> 04 cmpra,0h
06 jne02
08 ???

25
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
02 subra,1h
04 cmpra,0h
-> 06 jne02
08 ???

26
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
-> 02 subra,1h
04 cmpra,0h
06 jne02
08 ???

27
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
00 movra,3h
02 subra,1h
-> 04 cmpra,0h
06 jne02
08 ???

28
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0 Z
00 movra,3h
02 subra,1h
04 cmpra,0h
-> 06 jne02
08 ???

29
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0 Z
00 movra,3h
02 subra,1h
04 cmpra,0h
06 jne02
-> 08 ???

30
Loop with conditional jump (jne)
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0 Z
00 movra,3h
02 subra,1h
04 cmpra,0h
06 jne02
-> 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--) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
->00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

37
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0
00 movra,3h
-> 02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

38
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0
00 movra,3h
02 cmpra,0h
-> 04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

39
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
-> 06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

40
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
-> 0A subra,1h
0C jmp02
0E ???

41
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
-> 0C jmp02
0E ???

42
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
-> 02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

43
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
02 cmpra,0h
-> 04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

44
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
-> 06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

45
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
-> 0A subra,1h
0C jmp02
0E ???

46
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
-> 0C jmp02
0E ???

47
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
-> 02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

48
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
02 cmpra,0h
-> 04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

49
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
-> 06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

50
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
-> 0A subra,1h
0C jmp02
0E ???

51
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
-> 0C jmp02
0E ???

52
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
00 movra,3h
-> 02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

53
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0 Z
00 movra,3h
02 cmpra,0h
-> 04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
0E ???

54
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0 Z
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
-> 0E ???

55
for(i=3; i != 0; i--) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0 Z
00 movra,3h
02 cmpra,0h
04 je0eh
06 ...
08 ...
0A subra,1h
0C jmp02
-> 0E ???
Program continues execution

56
C to assembler loops
•for(i=3; i != 0; i--) {....}
•Alternative 1
->00 movra,3h
02 cmpra,0h
04 jne08h
06 jmp0E
08 ...
0A subra,1h
0C jmp02
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++) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
-> 00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???

59
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
00 movra,0h
-> 02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???

60
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
C
00 movra,0h
02 cmpra,3h
-> 04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???

61
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
C
00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
-> 08 ...
0A addra,1h
0C jmp02
0E ???

62
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
0 0 0 0
C
00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
-> 0A addra,1h
0C jmp02
0E ???

63
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
C
00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
-> 0C jmp02
0E ???

64
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
C
00 movra,0h
-> 02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???

65
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
1 0 0 0
C
00 movra,0h
02 cmpra,3h
-> 04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???
fast forw
ard som
e steps...

66
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
2 0 0 0
C
00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
-> 0A addra,1h
0C jmp02
0E ???

67
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0
C
00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
-> 0C jmp02
0E ???

68
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0
C
00 movra,0h
-> 02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???

69
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0 Z

00 movra,0h
02 cmpra,3h
-> 04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???

70
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0 Z

00 movra,0h
02 cmpra,3h
04 jb08h
-> 06 jmp0E
08 ...
0A addra,1h
0C jmp02
0E ???

71
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0 Z

00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
-> 0E ???

72
for(i=0; i <3; i++) {....}
PCAdrInstr RA RB RC RD
FLAGS
3 0 0 0 Z

00 movra,0h
02 cmpra,3h
04 jb08h
06 jmp0E
08 ...
0A addra,1h
0C jmp02
-> 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