Chapter 3 Assembly Language Fundamentals Slides to Accompany
Chapter 3: Assembly Language Fundamentals Slides to Accompany Assembly Language for Intel-Based Computers, Third Edition Irvine: Assembly Language for Intel-Based Computers (1999)
Overview • • Basic Elements of Assembly Language Sample Hello Program Assembling, Linking and Debugging Data Allocation Directives Symbolic Constants Data Transfer Instructions Arithmetic Instructions Basic Operand Types Irvine: Assembly Language for Intel-Based Computers (1999)
Basic Elements of Assembly Language • Constants and Expressions – Numeric Literals – Character or String constant • Statements • Names – Labels for Variables – Code Labels Irvine: Assembly Language for Intel-Based Computers (1999)
Numeric Literals (Constants) • A numeric literal is any combination of digits, plus: – optional decimal point, exponent, sign – examples of decimal literals: 5234 5. 5 -5. 5 26. 0 E+05 +35 d Irvine: Assembly Language for Intel-Based Computers (1999)
Numeric Literals • Use a radix symbol (suffix) to select binary, octal, decimal, or hexadecimal 6 A 15 h ; hexadecimal 0 BAF 1 h ; leading zero required 32 q ; octal 1011 b ; binary 35 d ; decimal (default) Irvine: Assembly Language for Intel-Based Computers (1999)
Symbolic Constant (Integer) • Defined using the = operator • Must evaluate to a 16 -bit integer – or 32 -bit integer when. 386 directive used COUNT = 25 ROWS = 10 table. Pos = ROWS * 5 Irvine: Assembly Language for Intel-Based Computers (1999)
Constant Expression • Combination of numeric literals, operators, and defined symbolic constants – must be evaluated at assembly time – examples: 4 * 20 -3 * 4 / 6 ROWS - 3 ; (ROWS is a constant) COUNT MOD 5 Irvine: Assembly Language for Intel-Based Computers (1999)
Statements • Label, mnemonic, operands, comment – – one statement per line name and comment are optional operands depend on the instruction Example. . Irvine: Assembly Language for Intel-Based Computers (1999)
Statement Example mnemonic Label Operands ; Code Here: mov ax, count Comment ; Data, using DW directive my. Array dw 1000 h, 2000 h dw 3000 h, 4000 h Data name Irvine: Assembly Language for Intel-Based Computers (1999)
The Hello World Program title Hello World Program (hello. asm) ; This program displays “Hello, world!”. model small. stack 100 h. data message db “Hello, world!”, 0 dh, 0 ah, ’$’. code main proc mov ax, @data mov ds, ax mov ah, 9 mov dx, offset message int 21 h mov ax, 4 C 00 h int 21 h main endp end main Irvine: Assembly Language for Intel-Based Computers (1999)
Sample Hello Program program title (comment) title Hello World Program (hello. asm) ; This program displays “Hello, world!” comment line . model small memory model . stack 100 h set the stack size Irvine: Assembly Language for Intel-Based Computers (1999)
Sample Hello Program starts the data segment. data message db “Hello, world!”, 0 dh, 0 ah, ’$’ starts the code segment. code main proc mov ax, @data sets DS to the offset of the mov ds, ax data segment mov ah, 9 mov dx, offset message int 21 h calls DOS display function 9 mov ax, 4 C 00 h int 21 h main endp end main halts program Irvine: Assembly Language for Intel-Based Computers (1999)
Common Assembler Directives Irvine: Assembly Language for Intel-Based Computers (1999)
Assemble-Link-Execute Cycle Irvine: Assembly Language for Intel-Based Computers (1999)
Files Created by the Assembler and Linker Irvine: Assembly Language for Intel-Based Computers (1999)
Data Allocation Directives Irvine: Assembly Language for Intel-Based Computers (1999)
Define Byte (DB) • Create data having a byte attribute – one or more bytes may be defined together – can mix different data representations – can assign a starting value. data char 1 db 'A' signed 1 db -128 combo db 'A', -10, 30 h, 40 q, 1101 b list db 10, 20, 30, 40 a. String db "Hello there", 0 count db ? ; uninitialized Irvine: Assembly Language for Intel-Based Computers (1999)
DUP Operator • Use DUP to create an array – each element is assigned the same value . data array. B db 30 dup(10 h) array 2 db 10 DUP(? ) ; uninitialized array 3 db 5 Dup(0), 33 h Irvine: Assembly Language for Intel-Based Computers (1999)
Define Word (DW) • Create data having a word attribute – can mix different data representations – can use integer expressions – can contain 16 -bit offset of another variable. data word. Val dw 1234 h, 5000 h, 65535, -24 signed 1 db 35 * 4 a. String dw "AB" ptr 1 dw offset wordval Irvine: Assembly Language for Intel-Based Computers (1999)
Define Doubleword (DD) • Create one or more 32 -bit integers – doubleword attribute . data large. Val dd 12345678 h array dd 100 dup(? ) small. Vals dd -1, -2, -3, -4, -5 Irvine: Assembly Language for Intel-Based Computers (1999)
Symbolic Constants • Equal-sign Directive • EQU Directive • TEXTEQU Directive Irvine: Assembly Language for Intel-Based Computers (1999)
Equal-Sign Directive prod = 10 * 5 ; Evaluates an expression max. Int = 7 FFFh ; Maximum 16 -bit signed value min. Int = 8000 h ; Minimum 16 -bit signed value max. UInt = 0 FFFFh ; Maximum 16 -bit unsigned value string = ‘XY’ ; Up to two characters allowed count = 500 endvalue = count + 1 ; Can use a predefined symbol. 386 max. Long = 7 FFFFFFFh min. Long = 80000000 h max. ULong = 0 FFFFh ; Maximum 32 -bit signed value ; Minimum 32 -bit signed value ; Maximum 32 -bit unsigned value Irvine: Assembly Language for Intel-Based Computers (1999)
Using the Equal-Sign Directive Irvine: Assembly Language for Intel-Based Computers (1999)
Data Transfer Instructions • • • INC DEC MOV Operands with Displacements XCHG Instruction Irvine: Assembly Language for Intel-Based Computers (1999)
Exchanging Two Variables title Exchange Two Variables (Exchange. asm). model small. stack 100 h. data value 1 db 0 Ah value 2 db 14 h. code main proc mov ax, @data ; initialize DS register mov ds, ax mov al, value 1 ; load the AL register xchg value 2, al ; exchange AL and value 2 mov value 1, al ; store AL back into value 1 mov ax, 4 C 00 h ; exit program int 21 h main endp end main Irvine: Assembly Language for Intel-Based Computers (1999)
Arithmetic Instructions • • INC and DEC ADD SUB Flags affected by ADD and SUB Irvine: Assembly Language for Intel-Based Computers (1999)
INC and DEC • INC – adds 1 to destination operand • DEC – subtracts 1 from destination operand • (both) – operand can be either a register or variable – Carry flag not affected Irvine: Assembly Language for Intel-Based Computers (1999)
INC and DEC Examples. data membyte db 25 memword dw 36 double. Val dd 12340000 h. code inc al dec bx inc eax inc membyte inc memword dec double. Val Irvine: Assembly Language for Intel-Based Computers (1999)
ADD Instruction ADD destination, source • Adds source operand to destination operand • Affects the Carry, Overflow, Sign and Zero flags • source operand can be register, immediate value, or memory • destination operand can be register or memory • only one operand can be a memory operand Irvine: Assembly Language for Intel-Based Computers (1999)
ADD Instruction Examples . data membyte db 25 memword dw 36 double. Val dd 12340000 h. code add al, 5 add bx, ax add eax, edx add membyte, al add memword, bx add double. Val, edx Irvine: Assembly Language for Intel-Based Computers (1999)
SUB Instruction SUB destination, source • Subtracts source operand from destination operand • Affects the Carry, Overflow, Sign and Zero flags • source operand can be register, immediate value, or memory • destination operand can be register or memory • only one operand can be a memory operand Irvine: Assembly Language for Intel-Based Computers (1999)
SUB Instruction Examples . data membyte db 25 memword dw 36 double. Val dd 12340000 h. code sub al, 5 sub bx, ax sub eax, edx sub membyte, al sub memword, bx sub double. Val, edx Irvine: Assembly Language for Intel-Based Computers (1999)
Flags Affected by ADD and SUB After the instruction has executed, . . . • If the destination is zero, the Zero flag is set • If the destination is negative, the Sign flag is set • If there was a carry out of the highest bit of the destination, the Carry flag is set • If the signed result is too small or too large to fit in the destination, the Overflow flag is set Irvine: Assembly Language for Intel-Based Computers (1999)
Signed Overflow • Signed overflow occurs when adding two signed integers, if and only if. . . – both operands are positive, or both operands are negative – and the sign of the sum is opposite to the sign of the values being added mov al, +127 add al, +1 ; AL = 80 h (-128), Overflow Irvine: Assembly Language for Intel-Based Computers (1999)
Signed Overflow Examples mov al, +127 add al, +1 ; AL = 80 h (-128), Overflow mov dx, -32768 ; DX = 8000 h (-32768) add dx, -1 ; DX = 8001 h, Overflow mov dx, -32768 ; DX = 8000 h (-32768) sub dx, 1 ; DX = 8001 h, Overflow Irvine: Assembly Language for Intel-Based Computers (1999)
Basic Operand Types Irvine: Assembly Language for Intel-Based Computers (1999)
The End Irvine: Assembly Language for Intel-Based Computers (1999)
- Slides: 37