0% found this document useful (0 votes)
12 views

Lecture5-80x86 Assembly Programming I

Uploaded by

saminn2442
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)
12 views

Lecture5-80x86 Assembly Programming I

Uploaded by

saminn2442
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/ 14

10/6/2024

Lecture 5: 80x86 Assembly


Programming I
Seyed-Hosein Attarzadeh-Niaki

Based on the slides by Hongzi Zhu

Microprocessors and Interfacing 1

Review
• 80x86 memory organization
– Memory segments
• Addressing modes

Microprocessors and Interfacing 2

1
10/6/2024

Outline
• Assembly statement
• Model definition
• Segments definition
• Building programs
• Data movement instructions
• Control transfer instructions
– Short, near and far
• Data types and definition

Microprocessors and Interfacing 3

Assembly Language Programs


• A series of statements (lines)
– Assembly language instructions (ADD, MOV, etc.)
• Perform the real work of the program
– Directives (pseudo-instructions)
• Give instructions for the assembler program about how to
translate the program into machine code.
• Consists of multiple segments
– CPU can access only one data segment, one code
segment, one stack segment and one extra segment
(Why? )

Microprocessors and Interfacing 4

2
10/6/2024

Form of a Statement
[label:] mnemonic [operands] [;comment]
• label is a reference to this statement
– Rules for names: each label must be unique; letters, 0-
9, (?), (.), (@), (_), and ($); first character cannot be a
digit; less than 31 characters
– “:” is needed if it is an instruction
• Mnemonic and the operands perform the real
work of the program.
• “;” leads a comment, the assembler omits
anything on this line following a semicolon

Microprocessors and Interfacing 5

Example of an Assembly Program


• Full segment definition
– See an example later
• Simple segment definition using models

Microprocessors and Interfacing 6

3
10/6/2024

Model Definition
• The MODEL directive selects the
size of the memory model
– SMALL: code <= 64KB
data <= 64KB
– MEDIUM: data <= 64KB
code > 64KB
– COMPACT:code <= 64KB
data > 64KB
– LARGE: data > 64KB
(single set of data<64KB)
code> 64KB
– HUGE: data > 64KB
code > 64KB
– TINY: code + data < 64KB

Microprocessors and Interfacing 7

Simplified Segment Definition


• Simplified segment definition
– .CODE, .DATA, .STACK
– Only three segments can be defined
– Automatically correspond to the CPU’s CS, DS, SS

Microprocessors and Interfacing 8

4
10/6/2024

Segments, All at a Glance


• Stack segment
• Data segment
– Data definition
• Code segment
– Write your statements
– Procedures definition
label PROC [FAR|NEAR]
label ENDP
– Entrance proc should be FAR

Note: On program start, the OS


assigns CS and SS, the program must
initialize DS.

Microprocessors and Interfacing 9

Sample Shell of an Assembly Program

Microprocessors and Interfacing 10

5
10/6/2024

Full Segment Definition


• Full segment definition
label SEGMENT
label ENDS
– You name those labels
– as many as needed
– DOS assigns CS, SS
– Program assigns DS (manually
load data segments) and ES

Microprocessors and Interfacing 11

Program Execution
• Program starts from the
entrance
– Ends whenever calls 21H
interrupt with AH = 4CH
• Procedure caller and callee
– CALL procedure
– RET

Microprocessors and Interfacing 12

6
10/6/2024

Build up Your Program


• .asm: the source file
• .obj: object file created by
assembler
• .lst: lists opcodes, offset
addresses and detected
errors
• .crf: cross reference file lists
references and lables and
their addresses
• .map: name of the
segments, their address and
size

Microprocessors and Interfacing 13

Assemble and Link


• The assembler program converts a symbolic source
module (file) into a hexadecimal object file.
ml new.asm
– The version that comes with Visual C will not work for 16-
bit DOS programs
• The linker program, reads the object files created by
the assembler and links them together into a single
execution file
• To use a library called NUMB.LIB while assembling a
program called NEW.ASM, type
ML NEW.ASM /LINK NUMB.LIB
Microprocessors and Interfacing 14

7
10/6/2024

Data Movement Instructions


Transfer data between Instructions
• registers, • Data movement instructions
• register and memory, include
– MOV, PUSH, POP, XCHG, XLAT,
• register and the stack,
IN, OUT, LEA, LOS, LES, LSS,
• memory and the stack, LGS, LFS, LAHF, SAHF
• accumulator and I/O, • String instructions
• flags and stack. – LODS, STOS, MOVS, INS, and
OUTS.

➢ Memory-to-memory only
with MOVS
Check the reference for more details
Microprocessors and Interfacing 15

Control Transfer Instructions


• Range
– SHORT, intrasegment
• IP changed: one-byte range (within -128 to
+ 127 bytes of the IP)
– Near, intrasegment
• IP changed: two-bytes range (±32K bytes)
• If control is transferred within the same
code segment
– FAR, intersegment
• CS and IP all changed
• If control is transferred outside the current
code segment
• Jumps
• CALL statement

Microprocessors and Interfacing 16

8
10/6/2024

Conditional Jumps
• Jump according to the value of the flag
register
• Short jumps
• Example:

Microprocessors and Interfacing 17

Unconditional Jumps
• JMP [SHORT|NEAR|FAR PTR] label
• Near by default
• In FAR jump, both IP and CS change
– another way to obtain a far jump is to define a
label as a far label
– The JMP UP instruction references a far label.
• label UP is defined as a far label by the EXTRN UP:FAR
directive

Microprocessors and Interfacing 18

9
10/6/2024

Subroutines & CALL Statement


• PROC & ENDP are used to define a subroutine
– Start and end of the procedure, require a label
• PROC is followed by a range definition
– NEAR: procedure is defined within the same code segment with
the caller (most procedures)
• Local: can be used by the current program
– FAR: procedure is defined outside the current code segment of
the caller • In MASM version 6.x, the NEAR
• Global: can be used by any program or FAR type can be followed by
the USES statement.
• USES allows any number of
• CALL is used to call a subroutine registers to be automatically
– Pushes the return address to stack pushed to and popped from
– RET is put at the end of a subroutine the stack within the procedure
– Difference between a far and a near call?
Microprocessors and Interfacing 19

Calling a NEAR Proc

Microprocessors and Interfacing 20

10
10/6/2024

Calling a FAR Proc

Microprocessors and Interfacing 21

Data Types & Definition


• CPU can process either 8-bit or 16 bit ops
– What if your data is bigger?
• Directives
– ORG: indicates the beginning of the offset address
• E.g., ORG 10H
– Define variables:
• DB: allocate byte-size chunks
– E.g., x DB 12 | y DB 23H,48H |Z DB ‘Good Morning!’
| str DB “I’m good!”
• DW, DD, DQ
– EQU: define a constant
• E.g., NUM EQU 234
– DUP: duplicate a given number of characters
• E.g., x DB 6 DUP(23H) | y DW 3 DUP(0FF10H)

Microprocessors and Interfacing 22

11
10/6/2024

Example

Microprocessors and Interfacing 23

More about Variables


• For variables, they may have names
– E.g., luckyNum DB 27H, time DW 0FFFFH
• Variable names have three attributes:
– Segment value
– Offset address
Logical address
– Type: how a variable can be accessed (e.g., DB is byte-wise, DW is word-
wise)
• Get the segment value of a variable
– Use SEG directive (E.g., MOV AX, SEG luchyNum)
• Get the offset address of a variable
– Use OFFSET directive, or LEA instruction
– E.g., MOV AX, OFFSET time, or LEA AX, time

Microprocessors and Interfacing 24

12
10/6/2024

More about Labels


• Label definition:
– Implicitly:
• E.g., AGAIN: ADD AX, 03423H
– Use LABEL directive:
• E.g., AGAIN LABEL FAR
ADD AX, 03423H
• Labels have three attributes:
– Segment value:
– Offset address: Logical address
– Type: range for jumps, NEAR, FAR

Microprocessors and Interfacing 25

More about the PTR Directive


• Temporarily change the type (range) attribute of
a variable (label)
– To guarantee that both operands in an instruction
match
– To guarantee that the jump can reach a label
• E.g., DATA1 DB 10H,20H,30H ;
DATA2 DW 4023H,0A845H
……
MOV BX, WORD PTR DATA1 ; 2010H -> BX
MOV AL, BYTE PTR DATA2 ; 23H -> AL
MOV WORD PTR [BX], 10H ; [BX],[BX+1]←0010H
• E.g., JMP FAR PTR aLabel

Microprocessors and Interfacing 26

13
10/6/2024

.COM Executable
• One segment in total
– Put data and code all together
– Less than 64KB

Microprocessors and Interfacing 27

Next Lecture
• 8086 Assembly
– Addition and subtraction
– Multiplication and division (unsigned)
– BCD arithmetic
– Rotate instructions

Microprocessors and Interfacing 28

14

You might also like