Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 – 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 Segment n SS A000h * * * 1MB offset Segment 2 16 bit Segment registers DS 8000h Segment 1 1. 2. 3. 4. 1 MB requires 20 bit address CS 0000h Each segment is 64 KB Offset address is 16 bit or 2 byte 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). 0 16 32 48 64 . . . . . . 16x . . . Segment Plus Offset Determines Address To get the real address 1. Pad 0H at the end of segment register 2. Add the offset value CS Offset = 1000H = F000H 10000H F000H 1F000H DS Offset = 1234H = 245FH 12340H 245FH 1479FH 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. From Intel Microprocessor Default Segment and Offset Registers 1. If CS = 1400H and IP/EIP = 1200 H 2. The microprocessor access instruction from 14000 H+ 1200H = 15200H. Suppose 1. 1000H bytes of code 2. 190H bytes of data 3. 200H bytes of stack Allows relocation Figure 2-4: A memory system showing the placement of four memory segments Figure 2-5 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 Declaration Data1 db 10H ; define byte – 1 byte Data2 dw 1234H ; define word – 2 bytes Data3 db 10 dup(0) ; array initialized with 0 Data4 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 mov ds, ax mov mov mov mov mov ax, ch, bx, bp, si, bx 3Ah 0300h 0200h 0200h ; .startup ; mov ax, 12AFH mov [1002h], ax mov mov mov mov [bx], al [bx+si], bp cl, [bx+4] Array[bx+si], dl mov ax, 4C00H int 21H end ; .exit ; 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 DATA1 DATA2 DATA DW DW DW 50 50 50 ; DATA is a word with value 50 DUP (?) ; array of 50 uninitialized words DUP (0) ; array of 50 words initialized with 0 50 DATA1 DATA2 0 0 MOV 0 1 CX, 10 XYZ: ; statements ; statements LOOP XYZ 0 0 0 2 3 49 ; loop count Accessing memory MOV AX, 1020h MOV AX, [1020h] MOV [AX], 20 AX 1021h 1020h 1021h 0001h 0000h Example Program 1 Write a program that will initialize 50 bytes array with values 1 to 50 1 2 3 4 5 0 1 50 2 mList 49 .MODEL SMALL .DATA DB 50 DUP (?) .CODE .STARTUP ; setup array of 50 bytes ; start of code segment ; start of program MOV MOV MOV AX, 1 CX, 50 ; load counter with 50 BX, OFFSET mList ; address of DATAS array MOV INC INC LOOP .EXIT END [BX], AX AX BX AGAIN AGAIN: ; save values to array positions ; increment AX to next values ; increment BX to next elements ; repeat 50 times ; exit to DOS ; end program Example Program 2 Write a program that will initialize 50 bytes array in the following form 50 49 47 1 0 49 1 2 mList .MODEL SMALL .DATA DB 50 DUP (?) .CODE .STARTUP ; setup array of 50 bytes ; start of code segment ; start of program MOV MOV MOV AX, 150 CX, 50 ; load counter with 50 BX, OFFSET mList ; address of DATAS array MOV DEC INC INC LOOP .EXIT END [BX], AX AX BX AGAIN AGAIN: ; save values to array positions ; increment decrement AX to next values ; increment BX to next elements ; repeat 50 times ; exit to DOS ; end program Example Program 2, Alternate Way Write a program that will initialize 50 bytes array in the following form 50 49 47 1 0 49 1 2 mList .MODEL SMALL .DATA DB 50 DUP (?) .CODE .STARTUP ; setup array of 50 bytes ; start of code segment ; start of program MOV MOV MOV MOV AX, 1 DI, 49 CX, 50 ; load counter with 50 BX, OFFSET mList ; address of DATAS array MOV INC DEC LOOP .EXIT END [BX+DI], AX AX DI AGAIN AGAIN: ; save values to array positions ; increment AX to next values ; decrement DI ; repeat 50 times ; exit to DOS ; end program Example 3 Move array element 10H into array element 20H **** 0 1 2 0 Array 16 **** ARRAY 1 2 *** 32 **** 29H 15 16 .MODEL SMALL .DATA DB 16 DB 29H DB 30 .CODE .STARTUP MOV MOV MOV MOV MOV .EXIT END ***** 17 DUP (?) 32 ; setup array DUP (?) BX, OFFSET ARRAY DI, 10H AL, [BX + DI] DI, 20H [BX+DI], AL ; start of program ; address of ARRAY ; address element 10H ; get element 10H ; address element 20H ; save in element 20H ; exit to DOS ; end program Example 3-7: Intel Microprocessors - by Brey Example 3, Alternate Way Move array element 10H into array element 20H **** ARRAY 0 1 2 **** 29H 15 16 17 Example: 3-8, Brey 32 References • Section 2-2, Intel Microprocessors – by Brey • Ch 3, Intel Microprocessors – by Brey • Ch 6, 10 Assembly Language Programming – by Charles Marut