Assembly Language
Programming
Assembly Programming
• Assembly Language instruction consist of four fields
[label:] mnemonic [operands] [;comment]
• Labels
• See rules
• mnemonic, operands
• MOV AX, 6764
• comment
• ; this is a sample program
Model Definition
MODEL directive –selects the size of the memory model
• MODEL MEDIUM
• Data must fit into 64KB
• Code can exceed 64KB
• MODEL COMPACT
• Data can exceed 64KB
• Code cannot exceed 64KB
• MODEL LARGE
• Data can exceed 64KB (but no single set of data should exceed 64KB)
• Code can exceed 64KB
• MODEL HUGE
• Data can exceed 64KB (data items i.e. arrays can exceed 64KB)
• Code can exceed 64KB
• MODEL TINY
• Data must fit into 64KB
• Code must fit into 64KB
• Used with COM files
Segments
Segment definition:
The 80x86 CPU has four segment registers: CS, DS, SS, ES
Segments of a program:
.STACK ; marks the beginning of the stack segment
example:
.STACK 64;reserves 64B of memory for the stack
.DATA ; marks the beginning of the data segment
example:
.DATA1 DB 52H
;DB directive allocates memory in byte-size chunks
Segments
.CODE ; marks the beginning of the code segment
- starts with PROC (procedures) directive
- the PROC directive may have the option FAR or NEAR
- ends by ENDP directives
Assemble, Link, and Run Program
STEP INPUT PROGRAM OUTPUT
1. Edit the program keyboard editor [Link]
2. Assemble the program [Link] MASM or TASM [Link]
[Link]
[Link]
3. Link the program [Link] LINK or TLINK [Link]
[Link]
Assemble, Link, Run Files
.asm – source file
.obj – machine language file
.lst – list file
- it lists all the Opcodes, Offset addresses, and errors
that
MASM detected
.crf – cross-reference file
- an alphabetical list of all symbols and labels used in
the
program as well as the program line numbers in which they
are referenced
.map – map file
- to see the location and number of bytes used when
PAGE and TITLE directives
PAGE [lines],[columns]
• To tell the printer how the list should be printed
• Default mode is 66 lines per page with 80 characters per line
• The range for number of lines is 10 to 255 and for columns
is 60 to 132
TITLE
• Print the title of the program
• The text after the TITLE pseudo-instruction cannot be more
than 60 ASCII characters
Control Transfer Instructions
• NEAR – When control transferred to a memory location
within the current code segment
• FAR – When control is transferred outside the current code
segment
• CS:IS – This register always points to the address of the next
instruction to be executed.
• In a NEAR jump, IP is updated, CS remains the same
• In a FAR jump, both CS and IP are updated
Control Transfer Instructions
• Conditional Jumps – See Table 2-1
• Short Jump
– All conditional jumps are short jump
– The address of the target must be within –128 to +127 bytes
of the IP
– The conditional jump is a two-byte instruction:
• One byte is the opcode of the J condition
• The 2nd byte is between 00 and FF
256 possible addresses:
forward jump to +127
backward jump to –128
Control Transfer Instructions
• forward jump to +127:
• calculation of the target address:
• by adding the IP of the following instruction to the operand (see
page 65)
• backward jump to –128
• the 2nd byte is the 2’s complement of the displacement value
• Calculation of the target address:
• the 2nd byte is added to the IP of the instruction after the jump (see
Program 2-1, and page 65)
Control Transfer Instructions
• Unconditional Jumps – “JMP label” - When control is transferred
unconditionally to the target location label
SHORT JUMP – “JMP SHORT label”
NEAR JUMP – “JMP label”
FAR JUMP – “JMP FAR PTR label”
• CALL statements – A control transfer instruction used to call a
procedure
• In NEAR call IP is saved on the stack (see figure 2-5, page 67)
• In FAR call both CS and IP are saved on the stack
• RET – the last instruction of the called subroutine
Control Transfer Instructions
• Assembly Language Subroutine
one main program and many subroutines
main program – is the entry point from DOS and is FAR
subroutines – called within the main program
• can be FAR or NEAR
if after PROC nothing is mentioned, it defaults to NEAR
Data Types and Data Definition
• 80x86 data types
8-bit or 16-bit
Positive or negative
example1:
number 510(1012) will be 0000 01010
example2:
number 51410(10 0000 00102) will be 0000 0010 0000 0010
Data Types and Data Definition
• Assembler data directives
ORG (origin) – to indicate the beginning of the offset address
example:
ORG 0010H
DB (define byte) – allocation of memory in byte-sized chunks
example:
DATA1 DB 25 ;decimal
DATA2 DB 10001001B ;binary
DATA3 DB 12H ;hex
DATA4 DB ‘2591’ ;ASCII numbers
DATA5 DB ? ;set aside a byte
DATA6 DB ‘Hello’ ;ASCII characters
DATA7 DB “O’ Hi” ;ASCII characters
Data Types and Data Definition
• Assembler data directives
DUP (duplicate) – to duplicate a given number of characters
example:
DATA1 DB 0FFH, 0FFH, 0FFH, 0FFH ;fill 4 bytes with FF
Can be replaced with:
DATA2 DB 4 DUP(0FFH) ;fill 4 bytes with FF
DATA3 DB 30 DUP(?) ;set aside 30 bytes
DATA4 DB 5 DUP (2 DUP (99)) ;fill 10 bytes with 99
Data Types and Data Definition
• Assembler data directives
DW (define word) – allocate memory 2 bytes (one word) at a time
example:
DATA1 DW 342 ;decimal
DATA2 DW 01010001001B ;binary
DATA3 DW 123FH ;hex
DATA4 DW 9,6,0CH, 0111B,’Hi’ ;Data numbers
DATA5 DW 8 DUP (?) ;set aside 8 words
EQU (equate) – define a constant without occupying a memory location
example:
COUNT EQU 25
;COUNT can be used in many places in the program
Data Types and Data Definition
• Assembler data directives
DD (define doubleword) – allocate memory 4 bytes (2 words) at a time
example:
DATA1 DD 1023 ;decimal
DATA2 DD 01010001001001110110B ;binary
DATA3 DD 7A3D43F1H ;hex
DATA4 DD 54H, 65432H,65533 ;Data numbers
DQ (define quadwordequate) – allocate memory 8 bytes (4 words) at a
time
example:
DATA1 DQ 6723F9H;hex
DATA2 DQ ‘Hi’ ;ASCII characters
DATA3 DQ ? ;nothing
Data Types and Data Definition
• Assembler data directives
DT (define ten bytes) – allocates packed BCD numbers (used in
multibyte addition of BCD numbers)
example:
DATA1 DT 123456789123 ;BCD
DATA2 DT ? ;nothing
DATA3 DT 76543d ;assembler will convert decimal
number to hex and store it