Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Assembly Language
in Short Overview
8086 Memory
Architecture
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
5 Assembly Language Programming Microprocessors 6 Assembly Language Programming Microprocessors
Higher Addresses
B4
50 SP ← offset to first location in stack segment
12
CS register ← Code base address / 10h
Code
34 Run program
Segment
87 Fetch machine instructions from code segment (CS:IP)
IP register ← Code base address % 10h 2B
Copy code to memory from address CS:IP C8 Access data and stack
01 OS services (interventions)
89D801C82B87341250B441CD21
D8
89 ← CS:IP Loading program into memory
12
ES = DS register ← Data base address / 10h Assigning initial values for registers in 8086
Data 34
Copy data to memory from address DS:EA Segment 56 Adjusting pointers stored in program’s data segment
78 ← DS:EA
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
9 Assembly Language Programming Microprocessors 10 Assembly Language Programming Microprocessors
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
17 Assembly Language Programming Microprocessors 18 Assembly Language Programming Microprocessors
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
21 Assembly Language Programming Microprocessors 22 Assembly Language Programming Microprocessors
0000 C70600000300 MOV WORD [0000],3 ; define a 0000 C70600000300 MOV WORD [0000],3 ; define a
0006 C7060200FFFF MOV WORD [0002],FFFF ; define b 0006 C7060200FFFF MOV WORD [0002],FFFF ; define b
000C C70604000000 MOV WORD [0004],0 ; define c 000C C70604000000 MOV WORD [0004],0 ; define c
0012 833E00000A loop: CMP WORD [0000],A ; CMP a == 10 0012 833E00000A loop: CMP WORD [0000],A ; CMP a == 10
0017 7D__ JGE end ; if a>=10 then 002D 0017 7D__ JGE 002D ; if a>=10 then 002D
0019 A10000 MOV AX,[0000] ; AX ← a 0019 A10000 MOV AX,[0000] ; AX ← a
001C D1E0 SHL AX,1 ; AX ← 2*AX 001C D1E0 SHL AX,1 ; AX ← 2*AX
001E A30200 MOV [0002],AX ; b ← AX 001E A30200 MOV [0002],AX ; b ← AX
0021 050700 ADD AX,7 ; AX ← AX+7 0021 050700 ADD AX,7 ; AX ← AX+7
0024 A30400 MOV [0004],AX ; c ← AX 0024 A30400 MOV [0004],AX ; c ← AX
0027 FF060000 INC WORD [0000] ; a ← a+1 0027 FF060000 INC WORD [0000] ; a ← a+1
002B EB__ JMP loop ; back to 0012 002B EB__ JMP 0012 ; back to 0012
002D C3 end: RET ; return to DOS 002D C3 end: RET ; return to DOS
Header000000000000000000000000000000
C70600000300C7060200FFFFC70604000000
833E00000A7D14A10000D1E0A30200050700
A30400FF060000EBE5C3
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
25 Assembly Language Programming Microprocessors 26 Assembly Language Programming Microprocessors
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
String Processing without Labels Arithmetic: Stored Data, Variables, and Pointers
0100 JMP 0122 0100 LEA SI,[0106] 0123 LODSW
0104 JMP 010A 0124 IMUL WORD [BX]
0102 db "Line of Text",0,0,0,0
0126 MOV [BX+02],AX
0112 db "New Line",0,0,0,0,0,0,0,0 0106 DW 0000
0129 MOV [BX+04],DX
0108 DW 0000
0122 MOV DI,0080 ; destination 012C CMP WORD [SI],0
010A MOV [SI+2],DS 012F JNZ 0123
0125 LEA SI,[0102] ; source_1
010D LEA AX,[0132] 0131 RET
0129 CALL 0134 ; call copy
0111 CWD 0132 DW 1122,0
012C LEA SI,[0112] ; source_2 0112 MOV BX,10 0136 DW 1,2,3,4,5,6,7,8
0130 CALL 0134 ; call copy 0115 DIV BX 0146 DW 9,a,b,c,d,e,f,0
0133 RET ; end of main routine 0117 ADD [SI+2],AX
0134 LODSB ; AL ← [SI], SI++ 011A MOV [SI],DX
0135 STOSB ; [DI] ← AL, DI++ 011C LDS SI,[SI]
0136 CMP AL,00 011E LEA BX,[SI]
0138 JNZ 0134 0120 ADD SI,4
013A RET
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
29 Assembly Language Programming Microprocessors 30 Assembly Language Programming Microprocessors
load module
section .text
DS:EA
mov si, 0x81 ; SI <-- pointer to command tail data data
Increasing
Addresses
JMP
mov di, argv ; DI <-- pointer to command tail buffer and and
NEAR
L1: lodsb ; al <-- character from command tail stack stack SS:SP
cmp al, 0x20 ; ignore initial spaces (0x20 characters)
jne L2s code code
jmp L1 start_segment
PSP
L2: lodsb ; get file name character
L2s: cmp al, 0x0d ; end on 0x0d character DOS DS = CS = SS = ES
je L3 = start_segment - 10
stosb ; copy character to file name buffer File on Disk File in RAM
jmp L2
; Load Module
L3: Code+Data+Stack stored in *.com program file on disk
; user program
Copied to RAM at start_segment
Load (after 100h bytes of PSP)
;
end: MOV AH,4Ch ; exit Single 64 KB segment
INT 21h ; call DOS CS = DS = SS = ES
Program codes starts at IP = 0100 (after 100h bytes of PSP)
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
C:\nasm\programs\examples>nasm ex1.asm
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
41 Assembly Language Programming Microprocessors 42 Assembly Language Programming Microprocessors
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
load module
load module
SS:SP
Code N_stack Code CS Code ISS:SP SS:SP
Increasing
Increasing
Addresses
Addresses
CS:IP
stored part stored part stored part stored part
N_code ICS:IP CS:IP
of Data of Data of DS of Data
Data start_segment Data
Segment Segment
header PSP header PSP
start_segment
DS:0000
DOS DOS
File on Disk File in RAM File on Disk File in RAM
Load Module Parameters from header used to set CS and SS
Data+Code+Stack stored in *.exe program file on disk ICS (initial code segment) = paragraphs to code segment from start of load module
Load
Copied to RAM at logical address start_segment:0000 (after 100h bytes of PSP) Load
DOS sets CS ← start_segment + ICS
Header = paragraphs to code segment from start of RAM
Parameters used to determine program addresses (ISS, SP, ICS, IP, others) ISS (initial stack segment) = paragraphs to stack segment from start of load module
N_code (physical byte offset) → ICS:IP (logical offset) DOS sets SS ← start_segment + ISS
N_stack (physical byte offset) → ISS:SP (logical offset) = paragraphs to stack segment from start of RAM
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
PSP
start_segment = 1000h Main Data b1 0
100h bytes Header 200h
PSP Logical Address DS:0000
DS = int((10000h – 100h)/10h)
= 0FF0
Logical Address of data = DS:0100
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
53 Assembly Language Programming Microprocessors 54 Assembly Language Programming Microprocessors
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
61 Assembly Language Programming Microprocessors 62 Assembly Language Programming Microprocessors
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land
65 Assembly Language Programming Microprocessors 66 Assembly Language Programming Microprocessors
Spring 2011 Hadassah College Dr. Martin Land Spring 2011 Hadassah College Dr. Martin Land