lecture 2 basic operations and memory addressing modes
DESCRIPTION
Lecture 2 Basic Operations and Memory Addressing Modes. Presented By Dr. Rajesh Palit Asst. Professor, EECS, NSU Originally Prepared By Dr. Shazzad Hosain , EECS, NSU. Road Map. Memory Addressing Real Mode and Protected Mode Basic Elements of Assembly Language - PowerPoint PPT PresentationTRANSCRIPT
Lecture 2Basic Operations and Memory
Addressing ModesPresented By
Dr. Rajesh PalitAsst. Professor, EECS, NSU
Originally Prepared ByDr. Shazzad Hosain, EECS, NSU
Road Map
• Memory Addressing– Real Mode and– Protected Mode
• Basic Elements of Assembly Language– Identifiers, Reserved words, Constants– MOV / XCHG– Addition, Subtraction– Input/Output
• Array, Loop and Accessing Memory
Real Mode Memory Addressing• The first 1MB memory is Real memory or the
Conventional memory
16 bit Segment registers
***
Segment 1
Segment 2
Segment n
0000hCS
8000hDS
A000hSS
1MB
offset
1. 1 MB requires 20 bit address2. Each segment is 64 KB3. Offset address is 16 bit or 2 byte4. Actual address = segment address + offset address
Real Mode Memory Addressing
• Real mode operation allows to address 1MB of memory space – even for the Pentium microprocessor
• This first 1MB memory is called the real memory or the conventional memory
• A combination of segment and offset address access the real memory
• Segment registers contains the beginning address of any 64KB memory segment
• The offset address selects the any location within the 64KB memory space
Interpretation of Segment Address
• A segment address is multiple of 2n
• In real mode, n = 4 means when a segment register contains 2, the corresponding address in the physical memory is 32.
• For example, DS = 66 (42H), means in physical memory it is 1056 (420H).
016
32
48
64......16x...
Segment Plus Offset Determines Address
From Intel Microprocessor
To get the real address1. Pad 0H at the end of segment register2. Add the offset value
1. Since each segment is 64 K, the offset address can take maximum of FFFFH
2. Once, the beginning address is found in segment registers, ending address is calculated by adding FFFFH with the value of segment register after padding 0H after it.
10000H F000H1F000H
CS = 1000HOffset = F000H
12340H 245FH1479FH
DS = 1234HOffset = 245FH
Default Segment and Offset Registers
1. If CS = 1400H and IP/EIP = 1200 H2. The microprocessor access instruction from
14000 H+ 1200H = 15200H.
Figure 2-4: A memory system showing the placement of four memory segments
Suppose1. 1000H bytes of code2. 190H bytes of data3. 200H bytes of stack
Figure 2-5
Allowsrelocation
Road Map
• Memory Addressing– Real Mode and– Protected Mode – supports up to 4G bytes of
memory – will be discussed later on.• Basic Elements of Assembly Language– Identifiers, Reserved words, Constants– MOV/ XCHG– Addition, Subtraction
• Array, Loop and Accessing Memory
Variables
• Reserved Words– Instruction Mnemonics, such as MOV, ADD, MUL– Register Names, Directives– Operators, Predefined symbols
• Identifiers– Not case sensitive– First character must be letter or _– Not Reserved word
Variable DeclarationData1 db 10H ; define byte – 1 byteData2 dw 1234H ; define word – 2 bytes
Data3 db 10 dup(0) ; array initialized with 0Data4 db 20 dup(?); array uninitialized
Data5 db 2, 4, 5, 6
Line db 5,4, 3 dup (7, 2 dup(0), 9)Line db 5,4,7,0,0,9,7,0,0,9,7,0,0,9
Instruction: MOV / XCHG
• MOV dest, source– MOV reg, reg– MOV mem, reg– MOV reg, mem– MOV mem, immi– MOV reg, immi
• XCHG dest, source– XCHG reg, reg– XCHG reg, mem– XCHG mem, reg
Data Addressing Modes
Program: Data Addressing.model small.data Array db 20 dup (0)
.code
mov ax, @data ; .startupmov ds, ax ; mov ax, bx mov ch, 3Ahmov bx, 0300h mov bp, 0200hmov si, 0200h
mov ax, 12AFHmov [1002h], ax mov [bx], almov [bx+si], bpmov cl, [bx+4]mov Array[bx+si], dl mov ax, 4C00H ; .exit int 21H ;
end
Instruction:Add / Sub / INC / DEC
• ADD dest, source• SUB dest, source
• Source can be reg/mem/immi• Dest can be reg/mem• source mem and dest mem NOT allowed
• INC dest or DEC dest – Reg or Mem location
Input / Output
• Single character Input– AH = 1– Int 21H– The input char will be in AL
• Single character output– AH = 2– DL = output char– Int 21H
• Outputting String– AH = 9– DX = address of the string– string db 'NSU', 0DH, 0AH, '$'
Array, Loop and Accessing Memory
DATA DW 50 ; DATA is a word with value 50DATA1 DW 50 DUP (?) ; array of 50 uninitialized words DATA2 DW 50 DUP (0) ; array of 50 words initialized with 0
50DATA
DATA1
DATA20 1 2 3 490 0 0 0 0
MOV CX, 10 ; loop countXYZ:
; statements; statementsLOOP XYZ
MOV AX, 1020hMOV AX, [1020h]MOV [AX], 20
AX
1020h1021h
0000h0001h
1021h
Accessing memory
Example Program 1
.MODEL SMALL
.DATAmList DB 50 DUP (?) ; setup array of 50 bytes
.CODE ; start of code segment
.STARTUP ; start of program
MOV AX, 1MOV CX, 50 ; load counter with 50MOV BX, OFFSET mList ; address of DATAS array
AGAIN:MOV [BX], AX ; save values to array positionsINC AX ; increment AX to next valuesINC BX ; increment BX to next elementsLOOP AGAIN ; repeat 50 times.EXIT ; exit to DOSEND ; end program
Write a program that will initialize 50 bytes array with values 1 to 50
1 2 3 4 5 500 1 2 49
Example Program 2
.MODEL SMALL
.DATAmList DB 50 DUP (?) ; setup array of 50 bytes
.CODE ; start of code segment
.STARTUP ; start of program
MOV AX, 1MOV CX, 50 ; load counter with 50MOV BX, OFFSET mList ; address of DATAS array
AGAIN:MOV [BX], AX ; save values to array positionsINC AX ; increment AX to next valuesINC BX ; increment BX to next elementsLOOP AGAIN ; repeat 50 times.EXIT ; exit to DOSEND ; end program
Write a program that will initialize 50 bytes array in the following form
50 49 47 10 1 2 49
50
DEC decrement
Example Program 2, Alternate Way
.MODEL SMALL
.DATAmList DB 50 DUP (?) ; setup array of 50 bytes
.CODE ; start of code segment
.STARTUP ; start of program
MOV AX, 1MOV DI, 49MOV CX, 50 ; load counter with 50MOV BX, OFFSET mList ; address of DATAS array
AGAIN:MOV [BX+DI], AX ; save values to array positionsINC AX ; increment AX to next valuesDEC DI ; decrement DILOOP AGAIN ; repeat 50 times.EXIT ; exit to DOSEND ; end program
Write a program that will initialize 50 bytes array in the following form
50 49 47 10 1 2 49
Example 3Move array element 10H into array element 20H
**** ***** ***0 1 2 16 32
.MODEL SMALL
.DATAArray DB 16 DUP (?) ; setup array
DB 29HDB 30 DUP (?).CODE.STARTUP ; start of program
MOV BX, OFFSET ARRAY ; address of ARRAYMOV DI, 10H ; address element 10HMOV AL, [BX + DI] ; get element 10HMOV DI, 20H ; address element 20HMOV [BX+DI], AL ; save in element 20H.EXIT ; exit to DOSEND ; end program
****0 1 2 15
29H
16 17 32
****
Example 3-7: Intel Microprocessors - by Brey
ARRAY
Example 3, Alternate WayMove array element 10H into array element 20H
****0 1 2 15
29H
16 17 32
****
Example: 3-8, Brey
ARRAY
References
• Section 2-2, Intel Microprocessors – by Brey• Ch 3, Intel Microprocessors – by Brey• Ch 6, 10 Assembly Language Programming –
by Charles Marut