0% found this document useful (0 votes)
3 views26 pages

C02103 05 Assembly Language Programming

The document provides an overview of 8086 Assembly Language programming, covering essential topics such as tools, programming processes, and elements of assembly language programs. It explains the differences between assembly and machine language, the role of assemblers and linkers, and the structure of assembly source files. Additionally, it includes examples of directives, data defining directives, and software interrupts used for input/output operations.

Uploaded by

ajmainfayek233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views26 pages

C02103 05 Assembly Language Programming

The document provides an overview of 8086 Assembly Language programming, covering essential topics such as tools, programming processes, and elements of assembly language programs. It explains the differences between assembly and machine language, the role of assemblers and linkers, and the structure of assembly source files. Additionally, it includes examples of directives, data defining directives, and software interrupts used for input/output operations.

Uploaded by

ajmainfayek233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Basics of 8086 Assembly

Language Programming
CO 2103 Assembly Language

[email protected] CO 2103 1
Topics
• Tools in AL Programming
• AL Programming Process
• “Hello World!” in AL
• Skeleton in brief
• Elements of AL Programs
– Statement
– Label
– Directive / Pseudo-Opcode
• Software Interrupt (Function Call)

[email protected] CO 2103 2
Assembly vs Machine Language
(recap)
• Machine Language (Machine Codes) – what CPU
understands, in 0s and 1s only, BUT not readable
to human
• Assembly Language (Assembly Codes) –
representation of ML in symbolic form, for
improved readability to human, BUT not
readable to CPU
– uses some high-level (HL) notations
• Require translation from AL to ML before saving
into memory for CPU to process/execute

[email protected] CO 2103 3
Tools in AL Programming - 1
• Assembler – convert AL program into Machine Code
(ML)
– functions:
• fix memory location for symbolic addresses
• change mnemonics to machine codes
• resolve symbols into actual data
• generate information for the Linker including:
– entry point (i.e. where program execution should start)
– addresses of symbols declared in this file (which may be used in
other files)
– unresolved symbols (which may be defined in other files)
– can be regarded as a low-level compiler
– output is Object file (.obj): same format as a compiler output
– can also output Listing file (.lst): lists program in AL and ML

[email protected] CO 2103 4
Tools in AL Programming - 2
• Linker – combine one or more object files into
an executable file
– functions:
• merge fragments of data and codes from different
object files
• may change the offset of some of the symbols
• adjust all references to these symbols
• generate information for relocation
– may also extract object codes from a library file
– output is executable file (.exe)
• Text Editor (ASCII) – to create the AL source file
[email protected] CO 2103 5
AL Programming Process
• Edit source file – AL → .asm
• Assemble AL source file: AL into ML → .obj and .lst
• Link ML Object file(s): address relocation → .exe
• Load (run) executable: into available memory space

[email protected] CO 2103 6
AL Source File – hello.asm

[email protected] CO 2103 7
Elements of AL Source File - 1
• Constants and expressions
– numeric literals
– character or string constant
• AL directives
– memory defining directives
– model directive: .model
– segment directives: .data, .stack, .code
– program organising directives - title, proc, endp, end
• Comment statement begins with semicolon “;”
• AL Statement:
– first column may be optional label – reference to address or data
– second column may be op code or assembler directive
– last column may be comment beginning with semicolon “;”
– AL Statement format:
<label> <op code or directive> <operands> ;<comments>
message db "Hello, world!",0dh,0ah,'$' ;newline + eoc
main: mov ax,@data ; data segment

[email protected] CO 2103 8
Elements of AL Source File - 2
• A numeric literal is any combination of digits, plus optional decimal
point, exponent, sign
– use a radix symbol (suffix) to select binary, octal, decimal, or
hexadecimal
5234 6A15h ; hexadecimal
5.5 0BAF1h ; leading zero required
-5.5 32q ; octal
26.0E+05 1011b ; binary
+35d 35d ; decimal (default)

• Symbolic constants are defined using the EQU directive or


‘=‘ operator
– must evaluate to a 16-bit integer
– or 32-bit integer when .386 directive used
COUNT EQU 25
ROWS = 10
tablePos = ROWS * 5
[email protected] CO 2103 9
Elements of AL Source File - 3
• Symbolic constants can be defined with constant
expression - combination of numeric literals, operators,
and defined symbolic constants
– must be evaluated at assembly time
SIZE = 4 * 20
NUM = -3 * 4 / 6
NROW = ROWS - 3 ;ROWS is a constant
REM = COUNT MOD 5

[email protected] CO 2103 10
hello.asm explained - 1
• A comment stating the program function; you can also
start with TITLE directive to give a program title
• Assembler directive to reserve stack (size) in memory
• Assembler directive (Assembler dependent) to declare
the memory model used. Model information tells the
linker how to merge the various data and code
segments.
• Tiny data+code in one segment ≤64K (.com)
• Small data in one segment, code in one segment
• Compact data in multiple segments, code in one segment
• Medium data in one segment, code in multiple segments
• Large data and code in multiple segments
• Huge allow data segment to be larger than 64K
• Flat no segment, 32-bit addresses, protected mode only
(80386 and higher)
[email protected] CO 2103 11
hello.asm explained - 2
• Assembler directive that tells the assembler to assemble
the following instructions into a data segment.
• where the data are defined: good to place this after the code
• “message” is the label for the first memory address in
data segment – “message” will be assembled and linked
to a memory address
• db is data defining directives to define bytes to be stored at
locations starting at “message”
• the statement stores 16 bytes (1 byte of ASCII code for each
character) into the data segment
• other data defining directives: dw, dd, dq, dt (more on these
later)

[email protected] CO 2103 12
hello.asm explained - 3
• Assembler directive that tells the assembler the
following instructions into a code segment
• where the program is written
• Start (entry) of the program instructions:
• first two instructions initialize DS register to point to correct
Data Segment - @data is an immediate operand, which actual
value will be patched in by the OS during loading
• next three instructions set up and call the DOS function
(software interrupt routine) to display the characters starting at
location labeled with message, and ending with “$”
• last two instructions set up and call the DOS function to halt
the program and exit
• Assembler directive that marks the end of AL program

[email protected] CO 2103 13
Skeleton in brief
.model small/medium/… ;specify memory model
.stack size ;specify stack size
.data ;data segment
<data declaration> ;declare data (variables, etc)

.code ;code segment


proc1 PROC (near or far) ;declare procedure proc1
<statements> ;codes here – end with return instruction
proc1 endp ;end of procedure proc1

proc2 PROC (near or far) ;declare procedure proc2


<statements> ;codes here – end with return instruction
proc2 endp ;end of procedure proc2

main PROC ;begin of main program


<statements> ;codes here
main endp ;end of main program

end ;end of AL program


[email protected] CO 2103 14
Label
• Label (or Symbol or Identifier) is a name associated with
some particular value :
– memory address
– data (constant or variable)
• Analogy to variables in mathematical expressions, label
are used as variables in the program
• Provides the ability to represent some otherwise
incomprehensible value with a familiar, mnemonic,
name.
• In hello.asm:
– message, main are labels

[email protected] CO 2103 15
Rules for Label
• Label consists of a sequence of letters, digits, and
special characters, with the following restrictions:
– cannot begin with a numeric digit
– usually not case sensitive
– may contain any number of characters, however only
the first 31 are used
– _, $, ?, and @ symbols may appear anywhere within a
label
– $ and ? are special symbols; you cannot create a label
made up solely of these two characters.
– cannot match any name that is a reserved symbol

[email protected] CO 2103 16
Reserved %out
.287
.486
.186
.386
.486P
.286
.386P
.8086
.286P
.387
.8087

words
.ALPHA .BREAK .CODE .CONST
.CREF .DATA .DATA? .DOSSEG
.ELSE .ELSEIF .ENDIF .ENDW
.ERR .ERR1 .ERR2 .ERRB
.ERRDEF .ERRDIF .ERRDIFI .ERRE
.ERRIDN .ERRIDNI .ERRNB .ERRNDEF

• Apart from all .ERRNZ


.IF
.EXIT
.LALL
.FARDATA
.LFCOND
.FARDATA?
.LIST
.LISTALL .LISTIF .LISTMACRO .LISTMACROALL
valid 80x86 .MODEL
.NOLIST
.MSFLOAT
.NOLISTIF
.NO87
.NOLISTMACRO
.NOCREF
.RADIX

instruction .REPEAT
.SFCOND
.UNTIL
.UNTIL
.STACK
.UNTILCXZ
.SALL
.STARTUP
.WHILE
.SEQ
.TFCOND
.XALL

names and .XCREF


BYTE
.XLIST
CATSTR
ALIGN
COMM
ASSUME
COMMENT
DB DD DF DOSSEG
register names, DQ
ECHO
DT
ELSE
DW
ELSEIF
DWORD
ELSEIF1

words in the list ELSEIF2


ELSEIFE
END
ELSEIFB
ELSEIFIDN
ENDIF
ELSEIFDEF
ELSEIFNB
ENDM
ELSEIFDEF
ELSEIFNDEF
ENDP

on the right are ENDS


EXTERN
EQU
EXTRN
EVEN
EXTERNDEF
EXITM
FOR
FORC FWORD GOTO GROUP
reserved, i.e. IF
IFDEF
IF1
IFDIF
IF2
IFDIFI
IFB
IFE

cannot be used IFIDN


INCLUDE
IRP
IFIDNI
INCLUDELIB
IRPC
IFNB
INSTR
LABEL
IFNDEF
INVOKE
LOCAL

as label: MACRO
PAGE
NAME
POPCONTEXT
OPTION
PROC
ORG
PROTO
PUBLIC PURGE PUSHCONTEXT QWORD
REAL4 REAL8 REAL10 RECORD
REPEAT REPT SBYTE SDWORD
SEGMENT SIZESTR STRUC STRUCT
SUBSTR SUBTITLE SUBTTL SWORD
TBYTE TEXTEQU TITLE TYPEDEF
UNION WHILE WORD

[email protected] CO 2103 17
Directive / Pseudo-Opcode
• Directives are special instructions that provide
information to the assembler but do not generate any
code, e.g. segment directives, equ, assume, end. They are
not valid 80x86 instructions. They are messages to the
assembler, nothing else.
• Pseudo-Opcode is a message to the assembler, just like
an assembler directive. They are sometimes used
interchangeably . However, a pseudo-opcode will emit
object code bytes, e.g db, n dup(?). These instructions
emit the bytes of data specified by their operands but
they are not true 80x86 machine instructions.

[email protected] CO 2103 18
Data Defining Directives - 1
Mnemonic Description Bytes Operand
DB Define Byte 1 Byte
DW Define Word 2 Word
DD Define Doubleword 4 Doubleword
DF, DP Define Far Pointer 6 Far Pointer
DQ Define Quadword 8 Quadword
DT Define Tenbytes 10 Tenbyte
• Data types can be decimal (100), binary (100b), hexadecimal (100h)
or ASCII (‘100’ or “100”)
– no typing – up to the programmer to define type
– use radix (suffix) to select binary, octal, decimal or hexadecimal

[email protected] CO 2103 19
Data Defining Directives - 2

• Use comma to define a list of values


– can mix different representations (decimal, binary, hex, ASCII)
• “?” represents an uninitialised memory location (allocated)
• Word, doubleword and quadword data are stored in reverse byte
order (in memory)
Directive Bytes in memory
db 1234567h 67 45 23 01

[email protected] CO 2103 20
Data Defining Directives - 3
• DUP
– allows a sequence of storage locations to be defined (with same value) or
reserved (uninitialised)
db 40 DUP (?) ;reserve 40 bytes storage
db 30 DUP (10h) ;allocate 30 bytes storage, each initialized to 10h
• Equal sign “=“
Label = expression
– expression must be numeric
– can be redefined in program
count = 1
count = count * 2
• EQU
Label EQU expression
– expression can be string or numeric
– use < and > to specify a string
– cannot be redefined in the program
val1 EQU 7Fh
message EQU <This is a message>
[email protected] CO 2103 21
DOS Function Calls – IO
• AL can make use of software interrupt (function call) to
access the IO (keyboard and screen)
– these are useful predefined subroutine
• Called using INT instruction in AL program
INT n ;software interrupt number n
– INT 10h - video BIOS
– INT 14h - serial I/O
– INT 16h - keyboard BIOS
– INT 17h - printer services
– INT 1Ah - time of day
– INT 1Ch - user timer
– INT 21h - DOS services
[email protected] CO 2103 22
DOS Function Call – few e.g.
--------D-2100------------------------------- --------D-2107-------------------------------
INT 21 - DOS 1+ - TERMINATE PROGRAM INT 21 - DOS 1+ - DIRECT CHARACTER INPUT, WITHOUT ECHO
AH = 00h AH = 07h
CS = PSP segment Return: AL = character read from standard input
Notes: Microsoft recommends using INT 21/AH=4Ch for DOS 2+ Notes: does not check ^C/^Break
execution continues at the address stored in INT 22 after DOS performs standard input is always the keyboard under DOS 1.x, but may be
whatever cleanup it needs to do redirected under DOS 2+
if the PSP is its own parent, the process's memory is not freed; if if the interim console flag is set (see AX=6301h), partially-formed
INT 22 additionally points into the terminating program, the double-byte characters may be returned
process is effectively NOT terminated SeeAlso: AH=01h,AH=06h,AH=08h,AH=0Ah
not supported by MS Windows 3.0 DOSX.EXE DOS extender
SeeAlso: AH=26h,AH=31h,AH=4Ch,INT 20,INT 22
--------D-2108-------------------------------
INT 21 - DOS 1+ - CHARACTER INPUT WITHOUT ECHO
--------D-2101------------------------------- AH = 08h
INT 21 - DOS 1+ - READ CHARACTER FROM STANDARD INPUT, WITH ECHO Return: AL = character read from standard input
AH = 01h Notes: ^C/^Break are checked, and INT 23 executed if detected
Return: AL = character read standard input is always the keyboard under DOS 1.x, but may be
Notes: ^C/^Break are checked, and INT 23 executed if read redirected under DOS 2+
character is echoed to standard output if the interim console flag is set (see AX=6301h), partially-formed
standard input is always the keyboard and standard output the screen double-byte characters may be returned
under DOS 1.x, but they may be redirected under DOS 2+ SeeAlso: AH=01h,AH=06h,AH=07h,AH=0Ah,AH=64h
SeeAlso: AH=06h,AH=07h,AH=08h,AH=0Ah

--------D-2109-------------------------------
--------D-2102------------------------------- INT 21 - DOS 1+ - WRITE STRING TO STANDARD OUTPUT
INT 21 - DOS 1+ - WRITE CHARACTER TO STANDARD OUTPUT AH = 09h
AH = 02h DS:DX -> '$'-terminated string
DL = character to write Return: AL = 24h (the '$' terminating the string, despite official docs which
Return: AL = last character output (despite the official docs which state state that nothing is returned) (at least DOS 3.3-5.0)
nothing is returned) (at least DOS 3.3-5.0) Notes: ^C/^Break are checked, and INT 23 is called if either pressed
Notes: ^C/^Break are checked, and INT 23 executed if pressed standard output is always the screen under DOS 1.x, but may be
standard output is always the screen under DOS 1.x, but may be redirected under DOS 2+
redirected under DOS 2+ under the FlashTek X-32 DOS extender, the pointer is in DS:EDX
the last character output will be the character in DL unless DL=09h SeeAlso: AH=02h,AH=06h"OUTPUT"
on entry, in which case AL=20h as tabs are expanded to blanks
SeeAlso: AH=06h,AH=09h …. and more

[email protected] CO 2103 23
Must Know INT
• Note the two-line instructions to exit to DOS
– They will probably appear at the end of most, if not all,
programs you will write

; Minimal program: do nothing and exit to DOS


.model small
.stack 100
.data
.code
main: mov ax,4c00h ; halt the program and return
int 21h
end main

[email protected] CO 2103 24
AL programming exercises …
(next)

[email protected] CO 2103 25
Summary
• CPU understands ML, human understands AL
– requires translation between AL and ML
• AL Programming Process:
– Edit → Assemble → Link → Load (Run)
• Tools for AL Programming:
– Editor, Assembler, Linker
• Elements of AL Source File:
– Statement: label, op code, operand, directive, comment
– Directive: program organization, memory, data defining
• INT function calls – useful for IO

[email protected] CO 2103 26

You might also like