Download Note - Engenuics
Transcript
MPG LEVEL 1
Around line 109 you will see:
EXTERN main
which tells the assembler to look for the “main” symbol in another file. Without this and the
corresponding
PUBLIC main
in chapter7.s, the branch to main instruction found down at line 185 will not work
; Launch main
bl
main
For Chapter 7, “main” is still written in assembler and devboardasm.s holds a function that will
be called by the main program. So in total, this project has three source files plus the
accompanying header files. Some of these files will stay with us for the remainder of the
course, others will disappear. When C and assembler files are used together, you will get a
better idea of how all the pieces fit.
7.3 Simple structures in assembly: if-else
Though we will not be writing too much more code in assembly, it is good to see how some
basic C structures are done in assembler. Obviously there must be a way to implement the
very simple decision structures that you use with barely a thought when writing in C. The first
one we will look at is an if-else in the context of Chapter 7 code that will check if a button is
pressed.
if( BUTTON0 == 0 )
{
/* The active-low button is pressed, so do some stuff */
}
else
{
/* The active-low button is not pressed, so do some other stuff */
}
/* Continue the program */
The code above would be the general structure to check a button and respond to its current
state, which is exactly what is done in the Chapter 7 code. Follow through the C code carefully:
1. The test is made
2. If the test is true, then the code proceeds sequentially. If the test is false, the true code
is skipped over and the false code executes.
3. If the test was true, once the true code is done the false code is skipped and the
program continues. If the test was false, the false code executes and then moves in to
the continuation of the program.
To write this code in assembly, all of the steps described above have to be implemented,
including instructions to jump around code that is not executed. You also must know how to
access the data where the button state lives and the mechanism to decide if BUTTON0 == 0.
notes_mpgl1_chapter7.docx
Release 1.0
Page 4 of 39