Elements of Assembly Language: How To Develop Assembly Language Programs Directives in MASM
Elements of Assembly Language: How To Develop Assembly Language Programs Directives in MASM
Elements of Assembly Language: How To Develop Assembly Language Programs Directives in MASM
How
Contents
Types
and formats of statements in MASM Example program for how to assemble, link, and execute Structure of assembly language program Constant operands () and instruction operands
prompt for () two numbers and then find and display their sum.
world 4. Store the first number in memory; 5. Prompt for the second number; 6. Input ASCII characters representing the first number; 7. Convert the characters to a 2s complement double world 8. Add the first number to the second number; 9. Convert the sum to a string of ASCII characters; 10. Display a label and the characters representing the sum;
directives
Copy file
cr Lf EQU EQU 0dh 0ah ; carriage return character ; line feed
.STACK 4096
output prompt1 ; prompt for first number input string, 40 ; read ASCII characters atod string ; convert to integer mov number1, eax ; store in memory
output prompt2 ; repeat for second number input string, 40 atod string mov number2, eax mov eax, number1 ; first number to EAX add eax, number2 ; add second number dtoa sum, eax ; convert to ASCII characters output label1 ; output label and sum
END
up to 512
code)
Directives Tell the assembler to take some action , not
macros
Shorthand for a sequence of other
statements
Components of a statement
name:
example
to understand the assembly process Shows the source code, the object code and additional information
ml /c /coff /Zi /Fl example.asm
Offset address of Object code of statements each directives or instructions or instructions value assigned by directives Representing data in hexdicimal
Shows
operands
Directives:
Radix Changes the default number base Assembler assumes that a number is decimal
Constant operands
EQU
Instruction set
Option casemap:none
mode define
model memory mode, [call], [others]
Memory mode
Memory mode
Tiny
File type OS
Code seg.
Data seg.
.com
DOS
One seg.
Small
Medium Compact Large Huge flat
64KB
64KB 64KB 64KB 64KB 4GB
.exe
.exe .exe .exe .exe
DOS
DOS DOS DOS DOS
1
1 Multi. Multi. Multi. Same seg.
1
Multi. 1 Multi. Multi.
.exe(PE) Windows
filename
filename
Declare function
Function_name
.data?
Not initial data
.const
Define constant
.stack
[size]
Define stack
[LABEL]
The last statement in the program. Label shows the entry point of
the program.
Laboratory Exercise1
Build
the developing environment for programming using assembly language. Test HelloCon.asm and HelloWin.asm. Make a program complete the following function: s=x+y. Use debugger to prove you program is correct. Report:
Introduce your developing enviroment. Write your program. Introduce the debugging
operands
Addressing modes
Giving
in
MOV instruction
We
is in a register.
MOV eax , ebx ; ebx -> eax MOV eax , 100 ; 100 -> eax
8-bit registers: AH, AL and DL 16-bit registers: AX, BX and DI
Not allowed
Never mix an 8-bit register with a 16-bit register, an 8-bit register with a 32-bit register, or a 16-bit register with a 32-bit register because this is not allowed by the microprocessor and results in an error when assembled. A segment-to-segment register MOV instruction is not allowed. Changing the CS register with a MOV instruction is not allowed.
instruction. Note: Programmer often codes a symbol that is associated value BYTE 100with a BYTE, DWORD, or WORD directive in the data segment or with an MOV eax , value instruction in the code segment. direct mode ;;;;; Register mode The location corresponding to such a symbol will be relocatable so that the assembler listing value EQU 100 MOV eax , an value shows assembly-time address that may be adjusted later. immediate mode Register mode
operand
EAX, EBX, ECX, EDX, ESI, EDI can be used for register indirect addressing.(in data segment) Register EBP can be used for register indirect addressing an address in stack segment. ESP also can be used for register indirect addressing. But we seldom use it. For 16-bit: only use BX, SI, DI, BP
the size of the memory operand is ambiguous, the PTR directive must be used to give the size to the assembler.
MOV [ebx] , 0
Base-plus-index addressing
()
For 16-bit: use one base register (BX or BP), and one index register (DI or SI) to indirectly address memory.
MOV CX,[BX+DI] MOV CH,[BP+SI] ;16-bit, CX = DS:[BX+DI] ;8-bit, CH = SS:[BP+SI]
For 32-bit: allows the combination of any two 32-bit extended registers except ESP.
MOV CL,[EDX+EDI] ;8-bit, CL=DS:[EDX+EDI] MOV [EAX+EBX],ECX;32-bit, DS:[EAX+EBX] =ECX
In register relative addressing, the data in a segment of memory are addressed by adding the displacement ()to the contents of a base or an index register (BX, SI, DI, or BP).
MOV AX,[DI+100H] ;16-bit, AX = DS:[DI+100H] MOV ARRAY[SI],BL ;8-bit, ARRAY[SI] = BL
For 32-bit, any of the 32-bit general-purpose registers can be used in the register relative addressing.
MOV DI,[EAX+10] ;16-bit, DI = DS:[EAX+10] MOV ARRAY[EBX],EAX ;32-bit, ARRAY[EBX] = EAX
Adds a displacement, besides using a base register (BX or BP) and an index register (SI or DI), to form the memory address.
MOV DH,[BX+DI+20H] MOV AX,FILE[BX+SI] MOV LIST[BP+DI],CL MOV LIST[BP+SI+4],DH MOV EAX,FILE[EBX+ECX+2]
In 80386 and above, all the 32-bit extended registers are both base register and index register.
Scaled-index addressing
()
This data-addressing mode is unique to the 80386 and above. It uses one or two 32-bit registers to access the memory. The second register is multiplied by a scaling factor (1, 2, 4, or 8).
MOV MOV MOV MOV MOV EAX,[EBX+4*ECX] [EAX+2*EDI+100H],CX AL,[EBP+2*EDI-2] EAX,ARRAY[4*ECX] AL,[2*EBX]
A scaling factor of 1X is implied and need not be included in the assembly language instruction.
this part youselves, and do the exercises. If you have any question, please put up your hand and ask me or discuss with others.
Exercises
P45.
Exercises3.1 1, 2 P67. Exercises3.4 2, 3, 4 P72. Exercises3.5 1-32 P77. Exercises3.6 1-8 P80. Exercises3.7 1-6