Chapter 3Data Representation |
Modified: |
3.1 Basic Elements of Assembly Language
A Complete Assembly Language Program INCLUDE Irvine32.inc
.code
main PROC
mov
add
sub
callexit
eax, 1000h
eax, 4000h
eax, 2000h
DumpRegsmain ENDP
END main
3.1.1 Integer Constants
[{+|-}] digits [radix]
[] means optional
{} means choice of one
| means OR
Common Radix binary 01010b decimal -123 hexadecimal 0ABCDh
3.1.2 Integer Expressions
Precedence as in programming languages such as Java.
5 + -7 * 3 is -16
(5 + -7) * 3 is -6
3.1.3 Real Number Constants
[{+|-}] integer.[integer][exponent]
+3.14
-3.8E-1
3.1.4 Character Constants
'X'
"X"
3.1.5 String Constants
'XYZ'
"XYZ"
3.1.6 Reserved Words
Cannot use words with special meaning.
Mnemonics, operators, etc.
BYTE
WORD
ADD
etc.
3.1.7 Identifiers
Name variables, labels, constants, procedures, etc.
- Start with non-digit
- Not case-sensitive
- 1-247 characters
- Include letters, digits, _, @, ?, $
AbC
A$$@_5?$
@endif
3.1.8 Directives
Provide direction to assembler, not executed.
.Data Directive that following text is part of program data.
.Code Directive that following text is part of program code.
See Appendix A for complete list.
3.1.9 Instructions
[label:] mnemonic operand(s) [; comment]
Label is any valid identifier
: serves to mark a location in a program algorithm,
or location of a variable.
@while:
@endwhile:
Mnemonic is an instruction operation.
Add, Sub, etc.
Operands of the operation.
Add eax, 4
3.2 Adding Three Integers
Adding three integers INCLUDE Irvine32.inc
.code
main PROCmov eax, 1000h
add eax, 4000h
sub eax, 2000h
call DumpRegsexit
main ENDP
END main
Executing
Click in the left gutter next to the instruction where execution should break.
Select Debug | Start Debugging
A window should open (check the task bar) with the following.
3.3 Assembling, Linking and Running Programs
3.3.1 Assemble-Link-Execute Cycle
Assemble - checks syntax and generates machine code corresponding to data and code.
Link - combines machine code with external code from libraries to an executable form.
Execute - loads the executable into memory and starts execution.
Listing File
Microsoft (R) Macro Assembler Version 9.00.21022.08 12/13/09 20:57:23
.\main.asm Page 1 - 1
INCLUDE z:\d\c335\irvine\Irvine32.inc
00000000 .code
00000000 main PROC
00000000 B8 00001000 mov eax, 1000h
00000005 05 00004000 add eax, 4000h
0000000A 2D 00002000 sub eax, 2000h
0000000F E8 00000000 E call DumpRegs
exit
0000001B main ENDP
END main
3.4 Defining Data
Definition Type Example BYTE 8 bit unsigned X BYTE 0A3h SBYTE 8 bit signed Y SBYTE -128 WORD 16 bit unsigned Z WORD 0ABCDh SWORD 16 bit signed A SWORD -1234 DWORD 32 bit unsigned B DWORD 12345678h SDWORD 32 bit signed C SDWORD -12345678 FWORD 48 bit integer D FWORD 123456 REAL4 4 byte IEEE short real E REAL4 3.1417
3.4.9 Little Endian Order
WORD - Stored in reverse byte order.
1234h stored as 34 12.
DWORD - Stored in reverse byte order.
Example12345678h stored as 78 56 34 12.
.data Address 00405000 X WORD 15 00405002 Y BYTE 7 00405003 C BYTE 'C' 00405004 A2E BYTE 'ABCDE' 00405009 Z WORD 1, 2, 3 0040500F A BYTE 5 dup(9) 00405014 N DWORD 12345678h 00405018Address Contents ASCII
0x00405000 0f 00 07 43 41 42 43 44 ...CABCD
0x00405008 45 01 00 02 00 03 00 09 E.......
0x00405010 09 09 09 09 78 56 34 12 ....xV4.Arrays - occupy memory in sequential order.
A2E is an array definition of 5 bytes stored sequentially as:
0x00405000 0f 00 07 43 41 42 43 44 ...CABCD
0x00405008 45 01 00 02 00 03 00 09 E.......A2E starts at address 0x00405004
A2E array 0 1 2 3 4 'A' 'B' 'C' 'D' 'E' 41 42 43 44 45 Z is an array definition of 3 words stored sequentially as:
0x00405008 45 01 00 02 00 03 00 09 E.......
Z array 0 1 2 1 2 3 01 00 02 00 03 00
3.5 Symbolic Constants
Use when a constant has special meaning (e.g. Age, PI).
Define once, use throughout program makes changing a constant simple.
Example
.data
MYNAME EQU 'ABCDE'
PAYRATE EQU 15
A2D BYTE MYNAME.code
add eax, PAYRATE