lecture (05) x86 programming 4 - draelshafee.net fileassembler directives directives are...
TRANSCRIPT
Lecture (05)x86 programming ‐ 4
By:
Dr. Ahmed ElShafee
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١
TOC
• IA32 cont,..
• Segmentation
• Assembler Directives
• Format of console programs
• Practical deliverable 01
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢
•
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٣
Simple Memory Addressing Modes
• Normal (R) Mem[Reg[R]]
– Register R specifies memory address
mov eax, [ecx]
• Displacement D(R) Mem[Reg[R]+D]
– Register R specifies start of memory region
– Constant displacement D specifies offset
mov edx, 8[ebp]
•
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٤
Example 06
int _tmain(int argc, _TCHAR* argv[]){unsigned int src_arr[4]={12,34,56,78};unsigned int x1,x2,x3,x4;unsigned int des_arr[4];_asm{lea ebx,src_arrmov edx,[ebx]mov x1,edxmov edx,4[ebx]mov x2,edxmov edx,8[ebx]mov x3,edxmov edx,12[ebx]mov x4,edx
lea ebx,des_arrmov edx,x4mov [ebx],edxmov edx,x3mov 4[ebx],edxmov edx,x2mov 8[ebx],edx
mov edx,x1mov 12[ebx],edx}printf("x1=%d\n",x1);printf("x2=%d\n",x2);printf("x3=%d\n",x3);printf("x4=%d\n",x4);printf("des_arr[0]=%d\n",des_arr[0]);printf("des_arr[1]=%d\n",des_arr[1]);printf("des_arr[2]=%d\n",des_arr[2]);printf("des_arr[3]=%d\n",des_arr[3]);printf("Press any key to continue,...");char ch = getch();return 0;}
•
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٥
int _tmain(int argc, _TCHAR* argv[]){unsigned int x,y,z;printf("Enter X=");scanf("%d",&x);printf("Enter y=");scanf("%d",&y);_asm{push xpush ypop eaxpop edxadd eax,edxpush eaxpop z}printf("z=%d\n",z);printf("Press any key to continue,...");getch();return 0;}
Example 07
Segmentation
• Segmentation is used to allow relocation of programs, i.e. programs can be loaded on different memory areas and still run correctly.
• Segmentation is used in the 8088/8086 microprocessors to allow the generation of 20‐bit addresses using 16‐bit registers.
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٦
• In the Real Mode Operation a 20‐bit address (effective address) is obtained by shifting the segment address 4 bits to the left (X10H) and then adding the offset address
• The offset address is specified in the program. The segment address is specified by the operating system whenever the program is loaded.
• The code segment holds the machine codes of the program. The Instruction Pointer specifies the offset address in the code segment.Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٧
• The data segment holds the data used by the program. Most data references are specified in the data segment.
• The stack segment holds the stack of the program. The offset address in the stack segment is specified with the registers SP and BP.
• The extra segment is used as a data segment by some data movement instructions.
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٨
Example 01
If DS =1600H find the maximum area occupied by the data segment. Find also the effective address, if the offset address is 1F00H.
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٩
Solution 01
Starting address = DS X 10H = 1600H X 10H
= 16000H
Ending address = Starting address + FFFF
= 16000H + FFFFH = 25FFFH
Effective address= Segment:Offset = 1600:1F00
= Segment address X 10H + Offset
= 1600H X 10H + 1F00H
= 16000H + 1F00H = 17F00H
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٠
Assembler Directives
Directives are instructions given by the programmer to the assembler on how to assemble the program.
Directives are not part of the instruction set of the microprocessor.
The use of directives might vary from assembler to assembler.
Some of the MASM 32 assembler directives are:
– ORG (Origin): Tells the assembler where to store the machine code of the next instruction.
– EQU (Equate): Tells the assembler to assign a value to an identifier.
– SEGMENT: Tells the assembler to begin a new segment.
– ASSUME: Tell the assembler to associate a segment with a segment register.
– PROC (Procedure): Tells the assembler to begin a new procedure.
– MACRO: Assigns the sequence of instructions to an identifier.
– END: Ends a program, segment (ENDS), a procedure (ENDP), or a macro (ENDM).Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١١
> Data Definition Directives
• Data Definition directives tell the assembler to store the specified data in the next memory locations. Data that occupies more than one location is stored with the LSByte in the lower address.
• (DB) Define Byte (1 byte or 8 bits ‐ Same as BYTE)
• (DW) Define Word (2 bytes or 16 bits ‐ Same as WORD)
• (DD) Define Double Word (4 bytes or 32 bits ‐ Same as DWORD)
• (DQ) Define Quad Word (8 bytes or 64 bits ‐ Same as QWORD)
• (DT) Define Ten Bytes (10 bytes or 80 bits ‐ Same as TBYTE)
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٢
Code 01
Examples:
• ABC DB 26H ;ABC=26H
• XYZ DB 35H,87H,0A4H ;XYZ= {35H,87H,A4H}
• YOU DB ‘ahmd’ ;YOU = ahmd’
• VAL DW 1254H ;VAL = 1254H
• X1 DB ? ;X1 = UNSPECIFIED
• X4 DB 3 DUP(20H) ;X4 = {20H,20H,20H}
• N1 DB 123H ;INVALID
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٣
Code 02
• Show the content of the memory based on the following data definitions:
ORG 100H
VAL1 EQU 21H
AB10 DB 37
NEW DB 23H,56H,’$’
LOT DW 1245H
XY11 DB ‘NEXT’
A123 DD 123H
B561 DB 4 DUP(40H)
VAL2 DB VAL1
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٤
AB10
Label
NEW+0
NEW+1
NEW+2
LOT
XY11+0
XY11+1
XY11+2
A123
XY11+3
B561+1
B561+0
B561+2
B561+3
VAL2
DS:0100
Address
DS:0101
DS:0102
DS:0103
DS:0105
DS:0104
DS:0106
DS:0107
DS:0108
DS:010A
DS:0109
DS:010B
DS:010C
DS:010D
DS:010F
DS:010E
DS:0110
DS:0111
DS:0112
Content
solution
• Show the content of the memory based on the following data definitions:
ORG 100H
VAL1 EQU 21H
AB10 DB 37
NEW DB 23H,56H,’$’
LOT DW 1245H
XY11 DB ‘NEXT’
A123 DD 123H
B561 DB 4 DUP(40H)
VAL2 DB VAL1
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٥
Code 03; data definition; emu86
ORG 100H
JMP START
VAL1 EQU 21H
AB10 DB 37
NEW DB 23H,56H,’$’
LOT DW 1245H
XY11 DB ‘NEXT’
A123 DD 123H
B561 DB 4 DUP(40H)
VAL2 DB VAL1
START:
nop
END
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٦
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٧
Format of DOS programs
• All programs must have a code and a stack.
– Code is the part of the program that contains the instructions of the program.
– Stack is an area in the RAM used by the system to store return addresses, and by the programmer to store temporarily data. It is a Last In First Out (LIFO) buffer.
• Programs can also have a data area, where all data (variables) is stored.
• There are two basic types of programs:
– Commands (.COM). The data and the stack of the program are part of the Code segment. The stack is always located at the end of the segment. The first 256 bytes of the segment are reserved.
– Executable (.EXE). The code and stack and data of the program are located in different segments.
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٨
Format of the .COM programs
.MODEL SMALL
CSEG SEGMENT PARA 'CODE’ ;Start a Code segment
ASSUME CS:CSEG, DS:CSEG, SS:CSEG
ORG 100H ;Reserve first 256 locations
START: JMP MAIN ;Skip data area
{Place the data of the program here}
MAIN PROC NEAR ;Beginning of main procedure
{Place the code of the
program here}
RET ;Get return DOS address
MAIN ENDP ;End of main procedure
CSEG ENDS ;End of the segment
END START ;End of the program
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1١٩
Format of the .COM programs
.MODEL SMALL
.CODE
ORG 100H ;Reserve first 256 locations
START: JMP MAIN ;Skip data area
{Place the data of the program here}
MAIN PROC NEAR ;Beginning of main procedure
{Place the code of the
program here}
RET ;Get return DOS address
MAIN ENDP ;End of main procedure
END START ;End of the program
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢٠
Example 01.01; hello world; masm
CSEG SEGMENT PARA ‘CODE’ ;Start a Code segment
ASSUME CS:CSEG, DS:CSEG, SS:CSEG
ORG 100H ;Reserve first 256 locations
START: JMP MAIN ;Skip data area
MSG DB "Hello World 01",'$‘
MAIN PROC NEAR ;Beginning of main procedure
LEA DX,MSG
MOV AH,9
INT 21H
INT 20H
RET
MAIN ENDP ;End of main procedure
CSEG ENDS ;End of the segment
END START ;End of the program
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢١
Example 01; hello world; masm
.MODEL SMALL
.CODE
ORG 100H ;Reserve first 256 locations
START: JMP MAIN ;Skip data area
MSG DB "Hello World 01",'$‘
MAIN PROC NEAR ;Beginning of main procedure
LEA DX,MSG
MOV AH,9
INT 21H
INT 20H
RET
MAIN ENDP ;End of main procedure
END START ;End of the program
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢٢
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢٣
Compiling and linking
Masm 01.asm
ML –AT 01.obj
01.com
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢٤
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢٥
Thanks,..
Dr. Ahmed ElShafee, ACU : Fall 2018, Microprocessors 1٢٦