Sei sulla pagina 1di 25

CSC 2420 Tutorial 8

More and More about 8086


Assembly Language
Segment Registers

MS-DOS initializes two segment registers


CS : code segment - your main program
SS : your stack segment
you should initialize data segment register
MOV AX, DATA_SEG
MOV DS, AX
Segment Definition

All programs consist of one or more


segments
 Code Segment; Data Segment; Stack Segment
They are defined with:
<Segment_Name> segment (<align>) (<combine>) (<class>)
<code>
<Segment Name> ends
Segment Names

Segment directive requires a label


(segment name) in the label field.
segment name -> address of a segment.
Also need specify the segment's name in
the label field of the ENDS directive that
ends the segment.
Segments normally load into memory in
the order of your source file.
Typical three segments
CODE_SEG SEGMENT PARA PUBLIC 'CODE'
: :
CODE_SEG ENDS

DATA_SEG SEGMENT PARA PUBLIC 'DATA'


: :
DATA_SEG ENDS

STACK_SEG SEGMENT PARA STACK 'STACK’


DW 1024 dup (?)
STACK_SEGENDS
Align Type

The align parameter is optional


byte, word, para, or page.
To instruct the assembler, linker, and DOS to
load the segment on a byte, word, paragraph
or page boundary.
Default : paragraph (16 bytes).
BYTE: loads the segment into memory starting at
the first available byte after the last segment.
WORD: starts the segment at the first byte with
an even address after the last segment.
Combine Type

The combine type controls the order of the


segment.
keywords: PUBLIC, STACK or NONE.
The PUBLIC and STACK combine all the
segments with the same name, and join
into a single contiguous segment.
Class Type

Specifies the ordering of segments that do


not have the same segment name.
This operand consists of a symbol
enclosed by apostrophes.
Generally, use: CODE, DATA and STACK.
Assume Directive

Tell the assembler you have loaded the


specified segment register(s) with the
segment addresses of the special value.
ASSUME CS:<code segment name>, DS:<data
segment name>, SS:<stack segment name>
This directive does not modify any of the
segment registers, it simply tells the
assembler to assume the segment
registers are pointing at certain segments
in the program.
END directive

Terminates assembly language source


file.
End directive operand <entry point> tells
the MS-DOS where to transfer the control
when the program begins execution, as
shown by the following syntax:
END <entry point>
Only one module should specify the
starting location of the program.
Basic structure of Data Segment

Data segment - defines the data you will


use.
<variable name> DB | DW <value>
Variable name represents the name of
your variable.
DB or DW declares the size of the
variable
<value> is the initial value of that variable.
Data Segment

The first variable you place in the data segment


gets allocated storage at location DS:0. The
next variable gets the storage just beyond the
previous variable.
DB declares the byte size (2 hex digits)
DW declares the word size (4 hex digits)
The question mark '?' tells the assembler that
the variable should be left uninitalized when it
loads into the memory.
Number Base

'b' or 'B': a binary number. (0 or 1)


'D' or 'd': a decimal number (default)
'H' or 'h': a hexadecimal number.
All integer constants must begin with a
decimal digit, including hexadecimal
constants. To represent the number
'ABCD', you must specify as '0ABCDh'.
Array

For one dimension arrays, the address=


Address = Base Address + (Index * Element
Size)
an array is defined as
<arrayname> DB | DW <size> dup
(<element>)
<arrayname> - name of the array variable
<size> dup (<element>) - duplicate the object with
the size defined in <size>.
e.g. V3 DB 10 dup(?)
Array

<arrayname> DB | DW value1,
value2, value3, ……valuen
e.g. Integers DB 0, 1, 2, 3, 4
e.g. Strange DW 256 dup (0, 1,
2, 3)
access the element in the array, address=
[<arrayname>+<index>*<element size>]
e.g. to denote the array SUMMER[2] with
word size in assembly, you can
[SUMMER+2*2]
String

To declare the string, you can use the


following syntax
<stringname> DB '<string>-$’
<stringname> - name of the string
placing an ' inside a string, you must place a
pair of ' next to each other,
e.g.'I''m fine, thanks'
or using the other string delimiter
"I'm fine, thanks"
Procedure Definition

Define at least one procedure


<procedure name> PROC {FAR | NEAR}
: : ;***your (main/ sub) program here***
<procedure name> ENDP
<procedure name> - must be unique.
PROC - procedure begins
ENDP - procedure ends.
Far - procedure can be called outside
Near - procedure can be called only in this
procedure
More on Code Segment

JMP and LOOP


Unconditional Jump - JMP

Usage:
JMP {<label> | <address> | <register>}
START: MOV AX, 20h
JMP START
JMP AX => MOV IP, AX
Conditional Jump - JXX

Usage: JXX {<label> | <address>}


Good friend: CMP (compare)
CMP {destination},{source};destination-source (set
flags)
which takes the form:
CMP {<reg> | <mem>}, {<reg> | <mem> |
<imm>}
JXX cannot jump too far…
a one byte opcode followed by a one byte
displacement.
JXX

Definition Description Condition


Jump Based on Unsigned Data
JE / JZ Jump equal or jump zero Z=1
JNE / JNZ Jump not equal or jump not zero Z=0
JA / JNBE Jump above or jump not below/ equal C=0 & Z=0
JAE / JNB Jump above/ equal or jump not below C=0
JB / JNAE Jump below or jump not above/ equal C=1
JBE / JNA Jump below/ equal or jump not above C=1 or Z=1
JXX

Jump Based on Signed Data


JE / JZ Jump equal or jump zero Z=1
JNE / JNZ Jump not equal or jump not zero Z=0
JG / JNLE Jump greater or jump not less/ equal N=0 & Z=0
JGE / JNL Jump greater/ equal or jump not less N=0
JL / JNGE Jump less or jump not greater/ equal N=1
JLE / JNG Jump less/ equal or jump not greater N=1 or Z=1
JXX

Arithmetic Jump
JS Jump sign N=1
JNS Jump no sign N=0
JC Jump carry C=1
JNC Jump no carry C=0
JO Jump overflow O=1
JNO Jump not overflow O=0
JP / JPE Jump parity even P=1
JNP / JPO Jump parity odd P=0
Looping

LOOP <label>
1. You have to initial CX
2. Automatically decrement CX in every
LOOP call
MOV CX, 10
A: INC AX
: :
LOOP A
I want to use turbo debugger

Assemble with:
tasm /zi /la myprogram.asm
tlink /v myprogram.obj
Turbo debugger
td myprogram.exe

Potrebbero piacerti anche