Memory Address Segmentoffset address Base location segment logical
Memory Address • • • Segment-offset address Base location (segment) + logical location (offset) Example: For 32 -bits segment-offset address, 08 F 1: 0100 represents a 20 -bits absolute address 09010 0 8 F 1 (0) 0000 1111 0000 + 0 1 0 0 0 9 0 1 0
Floating-Point Unit • 80 -bit Registers 48 -bit Pointer Register ST(0) FPU Instruction Pointer ST(1) FPU Data Pointer Tag Register Control Register Status Register 16 -bit Control Register ST(7) Opcode Register
DUP Operator • db 20 dup(0) ; 20 bytes, all equal to zero • db 20 dup (? ) ; 20 un-initialized bytes • db 4 dup(“ABC”) ; 12 bytes: “ABCABC” • db 4096 dup(0);
Assembly Language • Basic elements • 1. Constant and expression • 3. 5 • - 3. 5 • 26. E+07
Integer Constants • • 26 1 Ah 1101 b 36 q 2 BH 43 Q 36 D 48 d decimal hexadecimal binary octal hexadecimal octal decimal
Constant Expression, Symbolic constants and Character or String Constants • • Constant expression 5 26. 5 4*20 -3*4/6 -2. 301 E+04 A symbolic constant rows = 5 columns = 10 Number = rows * columns Character or string constant ‘ABC’ ‘X’ “ This is a test” “The dog is black”
Assembly Language Statement • Consists of : • 1. A name, an instruction mnemonic, operands, and a comment. It has the following form: • [name] [mnemonic] [operands] [; comment] • Example: • call sub 1 ; transfer of control
Names • A name identifies a label, variable, symbolic, or a keyword. • What can be used as a name: • 1. A… Z a… z • 2. 0 -9 • 3. ? • 4. _ Underscore • 5. @ • 6. $ • Length limit----maximum of 247 characters (in MASM)
Variables and Labels • 1. A variable is a location in a program’s data area that has been assigned a name. • Such as: count 1 db 50 ; • message db “ Hello world!”, odh, 0 ah • 2. Label. If a name appears in the code area of a program, it is called a label. • Example: • Label. A: mov ax, 0 • mov bx, ffh •
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
Analyze the Program • • • . model small ----specify the program’s memory model For that model we can have: Tiny--- code + data less than 64 k Samll--- code <=64 k, data , <=64 k One code segment and one data segment Medium---Data <=64 k, code any size. Multiple code segment, one data segment Compact---Code <= 64 k, data any size. Multiple data segment, one code segment Large---Code >64 k, data >64 k both multiple segment Huge---Same as the large model, except that individual variables such as arrays may be large than 64 k Flat--- No segments. 32 -bits addresses are used for both code and data. ***Protected mode only
Standard Assembler Directives • • • end---end of program assembly endp--- end of procedure page---Set a page format for the listing file proc---Begin procedure title---Title of the listing file. code--- Mark the beginning of the code segment • . data-- • . model-- • . stack---Set the size of the stack segment
Data Allocation Directives • • • DB----------1 byte DW----------2 DD----------4 DF, DP--------6 ---define far pointer DQ----------8 define Quadword DT----------10 define 10 bytes
Examples • • Char 1 db ‘A’ Char 2 db “A’ – 10 Smallest db – 128 Max_unsign db 255 ; ASCII char. ; expression • Myval db ? Only reserve memory, no initializeers • List db 10, 20, 30, 40, 50
Characters and Integers are one and the same • • • Char db ‘A’ Hex db 41 h Dec db 65 Bin db 01000001 Oct db 101 q
Type mix is allowed • List db 10, 20, 41 h, 10011000 b • List 2 db 0 ah, 29 h, ‘a’, 22
Strings • Cstring db “Good morning”, 0 • Pstring db 12, “Good morning” • Longstring db “This is a long string, that” • db “ clearly is going to take” • db “ several lines to store”, 0
Symbolic Constants • Equate directives • They allow constants and literals to be given symbolic names. • 1. Equal-Sign Directive (redefinable) • The syntax is: name = expression • Examples: string = ‘XY’ • count = 500 • min. Int = 8000 h •
To assign a 32 bits integer---. 386 is required • . 386 • max. Long = 7 FFFFFFFh ; maximum 32 -bit signed value • min. Long = 80000000 h ; Minimum 32 -bit signed value • “=“ defined symbols can be redefined any number of times.
Example of Using Directives • • count =5 mov al , count mov dl , al count =10 mov cx , count = 2000 mov ax, count
EQU and TEXTEQU directives • • EQU is not redefinable TEXTEQU is redefinable Examples: maxint equ 32767; Numeric float 1 equ <3. 1416> ; String TEXTEQU syntax: name TEXREQU <text> name TEXTEQU textmacro
Intel chip Instructions • 1. MOV---Data transfer instruction • Instruction format: • MOV reg, reg • MOV mem, reg • MOV reg, mem • • MOV reg, immed MOV mem 16, segreg MOV reg 16, segreg MOV segreg, immed 16 MOV segreg, reg 16 Flag bits are not affected by MOV instruction ***
Examples • • • . data count db 10 total dw 4126 h big. Val dd 12345678 h. code mov al, bl mov bl, count mov count, 26 mov bl, 1
Examples—cont… • • • mov dx, cx mov bx, 8 FE 2 h mov total, 1000 h mov eax, ebx mov edx, big. Val ; 32 -bit memory to reg ***Pay attention the fact that operands size must match. Otherwise, the assembler will return an error for any size mismatch.
Example • . data • count dw 20 h ; 20 h---here really mean 0020 h • . code • mov al, count; error:
XCHG Instruction • • • XCHG reg, reg xchg ax, bx xchg eax, ebx XCHG reg, mem xchg ax, value 1 • XCHG mem, reg • xchg value 2, cx
Program example • • • • • 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 Xchg value 2, al Mov value 1, al Mov ax, 4 c 00 h Int 21 h Main endp End main; Question: Why don’t we use xchg value 1, value 2?
Arithmetic Instructions • • ADD and SUB instructions ADD reg, reg ADD mem, reg ADD reg, mem ADD reg, immed ADD mem, immed ADD accum, immed Flag O D I S Z A P C * * * • SUB has the similar instruction format
Examples • Mov ax, 10 • Sub ax, 10 ; AX=0, ZF =1 • Mov bx, 1 • Sub bx, 2 ; BC = FFFF, SF = 1 • Mov ax, 0 FFh • Add al, 1 ; AL = 00 , CF = 1
Basic Operand Types • • • There are three basic types of operands: 1. Immediate----a constant 2. Register-------one of the CPU’s register 3. Memory---a reference to a mem. Location There are six different types of memory operands: • Direct, direct-offset, register-indirect • Indexed, base-indexed, and base-indexed with displacement
Example • mov ax, [si] --- this is called register-indirect • Here si register hold the beginning address for a list of numbers. • The first number in the address will be moved to al and the second number will be moved into ah.
Immediate operands, Direct Operands, and Direct-Offset operands • • • 1. Immediate operands: Mov al, 10 ; ----a number Mov eax, 12345678 h ; ---a 4 -byte number Mov dl, ‘X’ ; --- a character Mov ax, (40 *50) ; arithmetic expression
Direct Operands • • . data Count db 20 Wordlist dw 1000 h, 2000 h Long. Val dd 11223344 h. code Mov al, Count Mov bx, Wordlist +2 Mov edx, Long. Val
Direct Offset Operands • • . data Array db 0 ah, 0 bh, 0 ch, 0 dh. code Mov al, array ; 0 ah- al Mov bl, array +1 ; 0 bh bl Mov cl, array + 2 ; 0 ch cl Mov dl, array +3 ; 0 dh dl
Another Example
IA – 32 Processor Architecture • IA – 32 processor have three basic mode of operations: • 1. Protected Mode • 2. Real-address Mode • 3. System Management Mode
Protected Mode • IA-32 processors have three basic modes of operation: Protected mode, Real-address mode and System Management mode. In addition, the Virtual-8086 mode is a special case of Protected mode.
1. Protected Mode • Protected mode is the native state of the processor, in which all instructions and features are available. • Programs are given separate memory areas (called segment), and the processor detects any attempt by a program to reference memory outside its assigned segment. • Each program can address up to 4 GB memory.
Virtual – 8086 Mode • While in Protected mode, the processor can directly execute Real-address mode software such as MS-DOS programs in a safe multitasking environment. • In other word, even is an MS-DOS program crashes, it will not affect other program running at the same time.
Real-address mode • It offers us a few more features, such as the ability to switch into other two modes. • Only 1 MB of memory can be addressed: • 00000 h-FFFFFh • The processor can only run one program a time.
System Management Mode • SSM-mode • It provides an operating system with a mechanism for implementing such functions as power management and system security. • These functions are usually implemented by computer manufacturers who want to customize the processor for a particular system setup.
Homework 11 • Write and run an assembly program under debug that will: • 1) Add numbers from 1 to 1000. • Note: you would need these following instructions: • A) jz – jump if zero • B) mov cx, number of times (1000 in Hex. ) • C) create the labels that tells where to go.
- Slides: 44