Introduction to 8086 Assembly
-
Upload
ayad-m-al-awsi -
Category
Documents
-
view
220 -
download
0
Transcript of Introduction to 8086 Assembly
-
7/24/2019 Introduction to 8086 Assembly
1/13
Introduction to 8086 Assembly Language
CS 272Sam Houston State UniversityDr. Tim McGuire
Structure of an assembly language program
Assembly language programs divide roughly into five sectionsheaderequatesdatabodyclosing
The Header
The header contains various directives which do not produce machine codeSample header:
%TITLE "Sample Header".8086.model small.stack 256
Named Constants
Symbolic names associated with storage locations represent addresses
Named constants are symbols created to represent specific valuesdetermined by an expressionNamed constants can be numeric or stringSome named constants can be redefinedNo storage is allocated for these values
Equates
Constant values are known as equatesSample equate section:
Count EQU 10
Element EQU 5Size = Count * ElementMyString EQU "Maze of twisty passages"Size = 0
= is used for numeric values onlyCannot change value of EQU symbolEQUated symbols are not variables
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
1 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
2/13
EQU expressions are evaluated where used; = expressions are evaluatedwhere defined
The Data Segment
Begins with the.datadirectiveTwo kinds of variables, initializedand uninitialized.
Initialized variables take up space in the program's code fileDeclare uninitialized variables after initialized ones so they do not take upspace in the program's code file
Reserving space for variables
Sample DATA SEGMENT
.datanumRows DB 25numColumns DB ?videoBase DW 0800h
DB and DW are common directives (define byte) and (define word)The symbols associated with variables are called labelsStrings may be declared using the DB directive:
aTOm DB "ABCDEFGHIJKLM"
Program Data and Storage
Pseudo-ops to define data or reserve storageDB - byte(s)
DW - word(s)DD - doubleword(s)DQ - quadword(s)DT - tenbyte(s)
These directives require one or more operandsdefine memory contentsspecify amount of storage to reserve for run-time data
Defining Data
Numeric data values100 - decimal100b - binary100h - hexadecimal'100' - ASCII"100" - ASCII
Use the appropriate DEFINE directive (byte, word, etc.)A list of values may be used - the following creates 4 consecutive words
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
2 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
3/13
DW 40Ch,10b,-13,0
A ? represents an uninitialized storage location
DB 255,?,-128,'X'
Naming Storage Locations
Names can be associated with storage locations
ANum DB -4 DW 17ONEUNO DW 1X DD ?
These names are called variablesANum refers to a byte storage location, initialized to FChThe next word has no associated name
ONE and UNO refer to the same wordX is an uninitialized doubleword
Arrays
Any consecutive storage locations of the same size can be called an array
X DW 040Ch,10b,-13,0Y DB 'This is an array'Z DD -109236, FFFFFFFFh, -1, 100b
Components of X are at X, X+2, X+4, X+6
Components of Y are at Y, Y+1, , Y+15Components of Z are at Z, Z+4, Z+8, Z+12
DUP
Allows a sequence of storage locations to be defined or reservedOnly used as an operand of a define directive
DB 40 DUP(?)DW 10h DUP(0)DB 3 DUP("ABC")DB 4 DUP(3 DUP (0,1), 2 DUP('$'))
Word Storage
Word, doubleword, and quadword data are stored in reverse byte order (inmemory)
Directive Bytes in StorageDW 256 00 01
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
3 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
4/13
DD 1234567h 67 45 23 01DQ 10 0A 00 00 00 00 00 00 00X DW 35DAh DA 35
Low byte of X is at X, high byte of X is at X+1
The Program Body
Also known as the code segmentDivided into four columns: labels, mnemonics, operands, and commentsLabels refer to the positions of variables and instructions, represented bythe mnemonicsOperands are required by most assembly language instructionsComments aid in remembering the purpose of various instructions
An example
Label Mnemonic Operand Comment--------------------------------------------------------- .data
exCode DB 0 ;A byte variablemyWord DW ? ;Uninitialized word var. .codeMAIN PROC mov ax,@data ;Initialize DS to address mov ds,ax ; of data segment jmp Exit ;Jump to Exit label mov cx,10 ;This line skipped!Exit: mov ah,04Ch ;DOS function: Exit prog mov al, exCode ;Return exit code value int 21h ;Call DOS. Terminate progMAIN ENDP ;End Program END MAIN ; and specify entry point
The Label Field
Labels mark places in a program which other instructions and directivesreferenceLabels in the code segment always end with a colonLabels in the data segment never end with a colonLabels can be from 1 to 31 characters long and may consist of letters,digits, and the special characters ? . @ _ $ %If a period is used, it must be the first characterLabels must not begin with a digit
The assembler is case insensitive
Legal and Illegal Labels
Examples of legal namesCOUNTER1
@character
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
4 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
5/13
SUM_OF_DIGITS
$1000
DONE?
.TEST
Examples of illegal namesTWO WORDS contains a blank
2abc begins with a digitA45.28 . not first characterYOU&ME contains an illegal character
The Mnemonic Field
For an instruction, the operation field contains a symbolic operation code(opcode)The assembler translates a symbolic opcode into a machine languageopcodeExamples are: ADD, MOV, SUB
In an assembler directive, the operation field contains a directive(pseudo-op)Pseudo-ops are not translated into machine code; they tell the assembler todo something
The Operand Field
For an instruction, the operand field specifies the data that are to be actedon by the instruction. May have zero, one, or two operands
NOP ;no operands -- does nothingINC AX ;one operand -- adds 1 to the contents of AXADD WORD1,2 ;two operands -- adds 2 to the contents ; of memory word WORD1
In a two-operand instruction, the first operand is the destination operand.The second operand is the source operand.For an assembler directive, the operand field usually contains moreinformation about the directive.
The Comment Field
A semicolon marks the beginning of a comment field
The assembler ignores anything typed after the semicolon on that lineIt is almost impossible to understand an assembly language programwithout good commentsGood programming practice dictates a comment on almost every line
Good and Bad Comments
Don't say something obvious, like
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
5 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
6/13
MOV CX,0 ;move 0 to CX
Instead, put the instruction into the context of the program
MOV CX,0 ;CX counts terms, initially 0
An entire line can be a comment, or be used to create visual space in a
program
;; Initialize registers; MOV AX,0 MOV BX,0
The Closing
The last lines of an assembly language program are the closing
Indicates to assembler that it has reached the end of the program andwhere the entry point is
MAIN ENDP ;End of program END MAIN ; entry point for linker use
END is a pseudo-op; the single "operand" is the label specifying thebeginning of execution, usually the first instruction after the.codepseudo-op
Assembling a Program
The source file of an assembly language program is usually named with anextension of .asm
edit myprog.asm
The source file is processed (assembled) by the assembler (TASM) toproduce an object file (.obj)
tasm myprog produces myprog.obj
The object file must be linked by the linker (TLINK) to produce an
executable file (.exe)
tlink myprogproduces myprog.exe
Dealing with Errors
TASMwill report the line number and give an error message for each errorit finds
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
6 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
7/13
Sometimes it is helpful to have a listing file (.lst), created by using TASMwith the -loptionThe .lstfile contains a complete listing of the program, along with linenumbers, object code bytes, and the symbol table
Using the Debugger
Useful for logic errors that the assembler missesSee the text for a complete tutorialYou do notneed to use the TDH386.SYS driver or the TD386.EXE debuggerwith the latest version of the assemblerTo use the debugger on myprog.asm
tasm /zi myprogtlink /v myprogtd myprog
.COM and .EXE files
The .COM code file format is a relic of the first version of MS-DOSNot recommended for general purposesAll code, data, and the stack occupy one 64K segment (Borland's "tiny"model).EXE code files are more efficient in use of RAMData and code occupy separate segmentsThe programmer is responsible for setting up the data and code segmentsproperly
Ending a Program
All programs, upon termination, must return control back to anotherprogram -- the operating systemUnder MS-DOS, this is COMMAND.COMThis is done by doing a DOS system call
Data Transfer Instructions
MOV destination,sourcereg, regmem, regreg, memmem, immedreg, immed
Sizes of both operands must be the samereg can be any non-segment register except IP cannot be the target registerMOV's between a segment register and memory or a 16-bit register arepossible
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
7 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
8/13
Examples
mov ax, word1
"Move word1to ax"Contents of register axare replaced by the contents of the memorylocation word1
xchg ah, bl
Swaps the contents of ahand blIllegal: mov word1, word2
can't have both operands be memory locations
Sample MOV Instructions
b db 4Fhw dw 2048
mov bl,dhmov ax,wmov ch,b
mov al,255mov w,-100mov b,0
When a variable is created with a define directive, it is assigned a defaultsize attribute (byte, word, etc)You can assign a size attribute using LABEL
LoByte LABEL BYTEaWord DW 97F2h
Addresses with Displacements
b db 4Fh, 20h, 3Chw dw 2048, -100, 0
mov bx, w+2mov b+1, ahmov ah, b+5mov dx, w-3
Type checking is still in effectThe assembler computes an address based on the expression
NOTE: These are address computations done at assembly time
MOV ax,b-1
will not subtract 1 from the value stored at b
eXCHanGe
XCHG destination,source
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
8 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
9/13
reg, regreg, memmem, reg
MOV and XCHG cannot perform memory to memory movesThis provides an efficient means to swap the operands
No temporary storage is neededSorting often requires this type of operationThis works only with the general registers
Arithmetic Instructions
ADD dest, sourceSUB dest, sourceINC destDEC destNEG dest
Operands must be of the same size
sourcecan be a general register, memory location, or constantdestcan be a register or memory location
except operands cannot both be memory
ADD and INC
ADD is used to add the contents oftwo registersa register and a memory locationa register and a constant
INC is used to add 1 to the contents of a register or memory location
Examples
add ax, word1
"Add word1to ax"Contents of register axand memory location word1 are added, andthe sum is stored inax
inc ah
Adds one to the contents of ahIllegal: add word1, word2
can't have both operands be memory locations
SUB, DEC, and NEG
SUB is used to subtract the contents ofone register from another registera register from a memory location, or vice versaa constant from a register
DEC is used to subtract 1 from the contents of a register or memory location
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
9 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
10/13
NEG is used to negate the contents of a register or memory location
Examples
sub ax, word1
"Subtract word1from ax"Contents of memory location word1 is subtracted from the contents of
register ax, and the sum is stored inaxdec bx
Subtracts one from the contents of bxIllegal: sub byte1, byte2
can't have both operands be memory locations
Type Agreement of Operands
The operands of two-operand instructions must be of the same type (byteor word)
mov ax, bh ;illegal
mov ax, byte1 ;illegalmov ah,'A' ;legal -- moves 41h into ah
mov ax,'A' ;legal -- moves 0041h into ax
Translation of HLL Instructions
B = A
mov ax,Amov B,ax
memory-memory moves are illegal
A = B - 2*A
mov ax,Bsub ax,Asub ax,Amov A,ax
Program Segment Structure
Data SegmentsStorage for variablesVariable addresses are computed as offsets from start of this segment
Code Segmentcontains executable instructions
Stack Segmentused to set aside storage for the stackStack addresses are computed as offsets into this segment
Segment directives
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
10 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
11/13
.DATA
.CODE
.STACK size
Memory Models
.Model memory_model
tiny: code+data
-
7/24/2019 Introduction to 8086 Assembly
12/13
The interrupt instruction is used to cause a software interrupt (system call)An interrupt interrupts the current program and executes a subroutine,eventually returning control to the original programInterrupts may be caused by hardware or software
int interrupt_number ;software interrupt
Output to Monitor
DOS Interrupts : interrupt 21hThis interrupt invokes one of many support routines provided by DOSThe DOS function is selected via AHOther registers may serve as arguments
AH = 2, DL = ASCII of character to outputCharacter is displayed at the current cursor position, the cursor isadvanced, AL = DL
Output a String
Interrupt 21h, function 09hDX = offset to the string (in data segment)The string is terminated with the '$' character
To place the address of a variable in DX, use one of the followinglea DX,theString ;load effective address
mov DX, offset theString ;immediate data
Print String Example
%TITLE "First Program -- HELLO.ASM" .8086 .MODEL small
.STACK 256 .DATAmsg DB "Hello, World!$" .CODEMAIN PROC mov ax,@data ;Initialize DS to address mov ds,ax ; of data segment lea dx,msg ;get message mov ah,09h ;display string function int 21h ;display messageExit: mov ah,4Ch ;DOS function: Exit program mov al,0 ;Return exit code value int 21h ;Call DOS. Terminate programMAIN ENDP ;End of program END MAIN ; entry point
Input a Character
Interrupt 21h, function 01hFiltered input with echo
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
12 of 13 11/22/2015 10:29 A
-
7/24/2019 Introduction to 8086 Assembly
13/13
This function returns the next character in the keyboard buffer (waitingif necessary)The character is echoed to the screenAL will contain the ASCII code of the non-control character
AL=0 if a control character was entered
An Example Program
%TITLE "Case Conversion" .8086 .MODEL small .STACK 256 .DATAMSG1 DB 'Enter a lower case letter: $'MSG2 DB 0Dh,0Ah,'In upper case it is: 'CHAR DB ?,'$'exCode DB 0 .CODE
MAIN PROC;initialize ds mov ax,@data ; Initialize DS to address mov ds,ax ; of data segment;print user prompt mov ah,9 ; display string fcn lea dx,MSG1 ; get first message int 21h ; display it;input a character and convert to upper case mov ah,1 ; read char fcn int 21h ; input char into AL
sub al,20h ; convert to upper case mov CHAR,al ; and store it;display on the next line mov dx,offset MSG2 ; get second message mov ah,9 ; display string function int 21h ; display message and upper case;return to DOSExit: mov ah,4Ch ; DOS function: Exit program mov al,exCode ; Return exit code value int 21h ; Call DOS. Terminate programMAIN ENDP END MAIN ; End of program / entry point
Introduction to 8086 Assembly Language http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to
13 of 13 11/22/2015 10:29 A