Assembly Language Programming
Assembly Language Programming
Review
Outline
Flag instruction
Flag Register
Program
Effect of ADD on carry flag Use of zero flag for looping Add 5 bytes in debug Using MASM Simple directives Consider carry
Use ADC
Label - optional alphanumeric string 1st character must be a-z,A-Z,?,@,_,$ Last character must be : Operator - assembly language instruction mnemonic: an instruction format for humans Assembler translates mnemonic into hexadecimal opcode example: mov is f8h Operand[s] - 0 to 3 pieces of data required by instruction Can be several different forms Delineated by commas immediate, register name, memory data, memory address Comment - Extremely useful in assembler language These fields are separated by White Space (tab, blank, \n, etc.)
Operator
ax, bx
Operand[s]
;Comment
Not case sensitive Unlike other assemblers, destination operand is first mov is the mnemonic that the assembler translates into an opcode
Data
For static data Storage Source file defines size Source file defines content (optional) Can have 0 or more
Code
For machine Instructions Must have 1 or more
.ASM - Assembler source file prepared by programmer .OBJ - Translated source file by assembler .LST - Listing file, documents Translation process
Errors, Addresses, Symbols, etc
Executable Instr.: Instructions that the x86 can fetch from memory and execute MASM Dir.: Programmer supplied directives that guide the translation process MASM Macro Defs. and Calls:
ARITHMETIC/LOGIC
add fadd and ror ax, bx DAT ax, bx ax, 2 ;ax gets ax+bx ;ST get ST+DAT ;ax gets ax AND bx ;ax contents shifted-2 right
CONTROL TRANSFER
jnz int SUB1 LAbel LABEL1 21h ;if ZF=1 then IP=LABEL1 ;invoke INT handler 21h
Floating Point
Special Protected Mode
8087
80386
MMX (DSP)
Pentium MMX
SSE (Streaming SIMD Extension) - Special SIMD Pentium III Others (few specialized added with each generation)
SEGMENT BYTE 'DATA' ;Define the data segment EQU 9 ;define a constant via EQU DB 'Hello World',13,10,'$' ;Define the character string ENDS
SEGMENT BYTE 'CODE' ;Define mov ax, SEG hellodat mov ds, ax mov ah, dos_print mov dx,OFFSET strng int 21h mov ax, 4c00h int 21h ENDS END START the Code segment ;ax <-- data segment start address ;ds <-- initialize data segment register ;ah <-- 9 DOS 21h string function ;dx <-- beginning of string ;DOS service interrupt ;ax <-- 4c DOS 21h program halt function ;DOS service interrupt
hellocod
hellocod END
.code START: mov mov mov mov int mov int ax, SEG strng ds, ax ah, dos_print dx,OFFSET strng 21h ax, 4c00h 21h ;ax <-- data segment start address ;ds <-- initialize data segment register ;ah <-- 9 DOS 21h string function ;dx <-- beginning of string ;DOS service interrupt ;ax <-- 4c DOS 21h program halt function ;DOS service interrupt
END
START
end of program, label is entry point begin a procedure; far, near keywords specify if procedure in different code segment (far), or same code segment (near) end of procedure set a page format for the listing file title of the listing file mark start of code segment mark start of data segment set size of stack segment
maxint equ
count equ
32767
10 * 20 ; calculate a value (200)
List of Programs
1)
a. b. c. d. e. f. g. h. i. j.
write a program in debug calculate the sum of 4 words (each 16 bit data) data are 1234H, 03FDH, 4FD3H and 11FFH data is located at offset address of 0, 2, 4, 6 code starts at address 10H the result should be stored in location 8,9 save your program including the data run the program and write the sum. In your email you should attach the file and write the sum value in your email. The name of the file should be your firstname_lastname.com Note that finding how to save the file in debug is your responsibility
2) a. b. c. d. e. f.
write an assembly program and the compile it using masm and link move data from location of memory to another. Source data is Salam your_first_name Put separate code and data segment for your code Put some spaces in your data segment for destination Use masm and link and include .asm, .lst, .exe files in your email. The name of the file should be hw2