f28pl1 programming languages lecture 3: assembly language 2

32
F28PL1 Programming Languages Lecture 3: Assembly Language 2

Upload: evan-lawson

Post on 14-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: F28PL1 Programming Languages Lecture 3: Assembly Language 2

F28PL1 Programming Languages

Lecture 3: Assembly Language 2

Page 2: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Running assembly programs on the ARM-MDK

• ARM-MDK does not simulate a bare CPU• oriented to embedded systems with memory,

peripherals etc• STM32-Discovery board is:

– STMicroelectronics - STM32F103VB

• assumes no operating system• must provide start up file

– code to initialise peripherals, interrupts etc

• STM32-Discovery uses:– STM32F10x.s – .s == assembler

Page 3: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Running assembly programs on the ARM-MDK

• start up file associates reset button on board with:; Reset HandlerReset_Handler PROC EXPORT Reset_Handler [WEAK] IMPORT __main LDR R0, =__main BX R0 ENDP

• ; text == comment• Reset_Handler – labelled procedure start• PROC – start of procedure• EXPORT Reset_Handler – make Reset_Handler visible

externally

Page 4: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Running assembly programs on the ARM-MDK

; Reset HandlerReset_Handler PROC EXPORT Reset_Handler [WEAK] IMPORT __main LDR R0, =__main BX R0 ENDP

• IMPORT – make external label __main visible internally• LDR R0,=__main – load R0 with address for __main• BX R0 – branch indirect on R0 i.e. set PC to address for __main from R0

• ENDP - end procedure – not a return!

Page 5: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Running assembly programs on the ARM-MDK

• ARM-MDK expects every project to include a main.c– usually defining a main function– compiler generates assembly with EXPORTed __main

label– start up then IMPORTs __main

• can’t embed Thumb2 assembly language in C• provide a dummy main.c containing only:/* main.c */• GJM’s main.c

Page 6: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Running assembly programs on the ARM-MDK

• provide own assembly language file with __main: AREA ASMmain, CODE

; Main__main PROC EXPORT __main

<assembly code>ENDL B ENDL ENDP

END

• AREA – introduces block of code or data – name + attributes• __main – labelled procedure start• EXPORT __main – make __main visible externally

Page 7: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Running assembly programs on the ARM-MDK

AREA ASMmain, CODE; Main__main PROC EXPORT __main

<assembly code>ENDL B ENDL ENDP

END

• ENDL B ENDL – endless loop at end of program• END – end of source file• template in GJM’s ASMmain.s

Page 8: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Create new project

• create a new folder for new project• copy dummy main.c to folder• copy ASMmain.s template to folder• run Keil μVision 4• Project -> New μVision Project– in new folder– give project same name

Page 9: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Create new project

• Select Device for Target -> STMicroelectronics -> STM32F103VB – OK

• Copy STM32 Startup Code to Project Folder and Add Project to File?– No

• Project Window now shows: +Target 1• select +• Project Window now shows: +Target 1+Source Group 1

Page 10: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Create new project

• select inner +• Project Window now shows:

+Target 1+Source Group 1• Source Group 1 /right mouse button…• …Add Files To Group ‘Source Group 1’• Add: STM32F10x.s • Add: main.c and (possibly renamed) ASMmain.s– Close

Page 11: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Compile/assemble

• ASMmain.s/double left mouse button• ASMmain.s opens in tabbed window• add your assembler code

• Project -> Build Target– details in lower window

• fix program and repeat until:“project.axf" - 0 Error(s), 0 Warning(s).

Page 12: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Execute program on simulator

• Debug -> Start/Stop Debug Session– OK

• View -> Register Window• View -> Disassembly Window• select STM32F10x.s tab

Page 13: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Execute program on simulator

• select reset button:

• yellow arrow points at: LDR R0,=__main• repeatedly select step button:

• watch indicated instruction & registers changing• to leave simulator:

Debug -> Start/Stop Debug Session

RST

Page 14: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Multiply & divide

MUL {Rd, }Rn,Rm Rd = Rn*Rm

UDIV {Rd,}Rn,Rm Rd = Rn/Rm• unsigned• rounds down i.e. 7/3 == 2

Page 15: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Expressions

• for: var1 = var2 op var3

• with:var1 RN R1

var2 RN R2

var3 RN R3

op R1,R2,R3

• e.g.x = y*z; x RN 1y RN 2z RN 3MUL x,y,z

Page 16: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Expressions

• for:var1 = var2 op1 var3 op2 var4

• if op1 and op2 have same precedence

• or op1 has higher precedence than op2

op1 R1,R2,R3

op2 R1,,R4

e.g.x = y*z-a; ...a RN 4MUL x,y,zSUB x,a

Page 17: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Expressions

• for:var1 = var2 op1 var3 op2 var4

• if op2 has higher precedence than op1

• must evaluate op2 expression in new register

op2 Ri,R3,R4

op1 R1,,R2,Ri

e.g.x = y+z*a; MUL R5,z,aADD x,y,R5e.g.x = y*(z+a) ADD R5,z,aMUL x,y,R5

Page 18: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Expressions

• draw expression tree• allocate registers to nodes – from bottom left– if expression in assignment then start with register

for destination variable• accumulate into register for left operand• can re-use any register whose value is no longer

required

Page 19: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagorus

int x; /* R1 */int y; /* R2 */int z; /* R3 */int p; /* R4 */x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

==

+

*

x x

*

y y

*

z z

Page 20: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagorus

int x; /* R1 */int y; /* R2 */int z; /* R3 */int p; /* R4 */x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

==

+

* R5

x x

*

y y

*

z z

Page 21: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagorus

int x; /* R1 */int y; /* R2 */int z; /* R3 */int p; /* R4 */x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

==

+

* R5

x x

* R6

y y

*

z z

Page 22: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagorus

int x; /* R1 */int y; /* R2 */int z; /* R3 */int p; /* R4 */x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

==

+ R5

* R5

x x

* R6

y y

*

z z

Page 23: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagorus

int x; /* R1 */int y; /* R2 */int z; /* R3 */int p; /* R4 */x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

==

+ R5

* R5

x x

* R6

y y

* R6

z z

Page 24: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagoras

int x; /* R1 */int y; /* R2 */int z; /* R3 */int p; /* R4 */x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

==CMP

+ R5

* R5

x x

* R6

y y

* R6

z z

Page 25: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagorasint x;int y;int z;int p;x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

AREA PYTH, CODE; Main__main PROC EXPORT __main

x RN 1y RN 2z RN 3p RN 4

MOV x,#3MOV y,#4MOV z,#5

Page 26: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: Pythagorasint x;int y;int z;int p;x = 3;y = 4;z = 5;if(x*x+y*y==z*z) p=1;else p=0;

MUL R5,x,x MUL R6,y,y ADD R5,R6

MUL R6,z,zCMP R5,R6BEQ SAMEMOV p,#0B ENDL

SAME MOV p,#1ENDL B ENDL ENDP

END

Page 27: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Structured programming

• assembly language requires intricate use of labels & branches

• easy to produce “spaghetti code”• design assembly programs using high level program

structures – condition– iteration

• use template to translate high level to label + branch

Page 28: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Structured programming: if

IF exp1 op exp2 THEN

command1

ELSE command2

not(=) NEnot(!=) EQnot(<) GEnot(<=) GTnot(>) LE not(>=) LT

Ri = exp1

Rj = exp2

CMP Ri,RjBnot(op)

IFALSEcommand1

B IENDIFALSE command2

IEND

Page 29: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Structured programming: if

IF exp1 op exp2 THEN

command

Ri = exp1

Rj = exp2

CMP Ri,RjBnot(op)

IENDcommand

IEND

Page 30: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Structured programming: while

WHILE exp1 op exp2 DO

command

WLOOP Ri = exp1

Rj = exp2

CMP Ri,Rj Bnot(op) WEND

commandB WLOOP

WEND

Page 31: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: is x prime?

• dreadful algorithm...!int x;int n;int p;x = 23;n = 2;p = 1;while(n<x) if(x%n==0) { p = 0; break; } else n = n+1;

AREA PYTH, CODE; Main__main PROC EXPORT __main

x RN 1n RN 2p RN 3

MOV x,#25MOV n,#2MOV p,#1

Page 32: F28PL1 Programming Languages Lecture 3: Assembly Language 2

Example: is x prime?

• dreadful algorithm...!int x;int n;int p;x = 23;n = 2;p = 1;while(n<x) if(x%n==0) { p = 0; break; } else n = n+1;

TEST CMP n,xBEQ ENDLUDIV R4,x,nMUL R4,nCMP R4,xBNE NOTPMOV p,#0B ENDL

NOTP ADD n,#1B TEST

ENDL B ENDL ENDP

END

n<x

x%n==0 (x/n)*n==x

n = n+1

p=0; break