lesson02.ppt

Download Report

Transcript lesson02.ppt

Let’s do a code ‘walk-through’
Here we examine the design
details for a short, but not trivial,
assembly language example
A good way to get us going…
•
•
•
•
•
It’s a short program (could fit on one page)
It’s ‘self-contained’ (i.e., needs no libraries)
It displays output that is helpful for CS
It illustrates important assembly principles
It implements a ‘nested loop’ algorithm
Source-statement format
• Each statement in an assembly language
program’s source-file follows a ‘standard’
format, comprised of four distinct fields:
label:
opcode
operand(s)
# comment
comma-separated list
whitespace
colon
hashmark
Labels
• The ‘label’ field is allowed to be blank if the
statement doesn’t need a label (i.e., if it’s
not referred to by any other statements)
• Where a label is needed, the programmer
is responsible to making up the name:
– It cannot begin with a digit-character (‘0’..’9’)
– It can only contain letters and digits, or the
three special characters: ‘.’, ‘_’, ‘$’
Two kinds of ‘opcodes’
• The ‘opcode field’ holds a reserved word
– An Intel-x86 ‘instruction’, or
– A GNU-assembler ‘directive’
• This distinction will be very important!
• But it’s easy to recognize the ‘directives’
because they begin with a dot-symbol (‘.’)
Operands vary with the opcode
•
•
•
•
•
Some opcodes require no operands
Some opcodes need only one operand
Most opcodes need two operands
A few opcodes need three operands
When more than one operand is needed,
the comma-symbol is used a separator
Comment-field
• The comments are programmer-designed
• Their intent is to help other programmers
understand the purpose of a statement in
the overall flow of the program-algorithm
• They are ignored by the assembler as far
as the language-translation is concerned
• But they will be displayed in an assembler
listing-file (if in fact one is generated)
Typical ‘counted-loop’ construct
Initialize some variables
Perform a loop-iteration
Adjust the iteration-counter
no
Done?
yes
A loop-within-a-loop
‘outer’
loop
‘inner’
loop
Demo: ‘nybbles.s’
• You can download this source-file from our
class website to your local directory:
$ cp /home/web/cruse/cs210/nybbles .
• You can assemble it and link it, like this:
$ as nybbles.s -o nybbles.o
$ ld nybbles.o -o nybbles
• Then you can execute it, like this:
$ ./nybbles