Microprocessor and Assembly Language Programming
Microlink Information Technology College
LAB-2
Objective: Students will learn Assembly programming pneumonic, and explore
simple Assembly Language Instructions in assembly and run them.
To run the program you need to follow these steps:
Lab Work:
As the first example we will see the Moves. Each student should run the program
and should try to trace it as well.
; Data Transfer Examples (Moves.asm)
; Chapter 4 example. Demonstration of MOV and
; XCHG with direct and direct-offset operands.
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:dword
.data
val1 WORD 1000h
val2 WORD 2000h
arrayB BYTE 10h,20h,30h,40h,50h
arrayW WORD 100h,200h,300h
arrayD DWORD 10000h,20000h
.code
main proc
; MOVZX
mov bx,0A69Bh
movzx eax,bx ; EAX = 0000A69Bh
movzx edx,bl ; EDX = 0000009Bh
movzx cx,bl ; CX = 009Bh
; MOVSX
mov bx,0A69Bh
movsx eax,bx ; EAX = FFFFA69Bh
movsx edx,bl ; EDX = FFFFFF9Bh
mov bl,7Bh
movsx cx,bl ; CX = 007Bh
; Memory-to-memory exchange:
mov ax,val1 ; AX = 1000h
xchg ax,val2 ; AX = 2000h, val2 = 1000h
mov val1,ax ; val1 = 2000h
; Direct-Offset Addressing (byte array):
mov al,arrayB ; AL = 10h
mov al,[arrayB+1] ; AL = 20h
mov al,[arrayB+2] ; AL = 30h
; Direct-Offset Addressing (word array):
mov ax,arrayW ; AX = 100h
mov ax,[arrayW+2] ; AX = 200h
; Direct-Offset Addressing (doubleword array):
mov eax,arrayD ; EAX = 10000h
mov eax,[arrayD+4] ; EAX = 20000h
mov eax,[arrayD+TYPE arrayD] ; EAX = 20000h
Invoke ExitProcess,0
main endp
end main
Lab Work -2 :
As the second example we will see the TYPE, LENGTHOF, and SIZEOF
operators. Each student should run the program and should try to trace it as well.
; Operators (Operator.asm)
; Demonstrates the TYPE, LENGTHOF, and SIZEOF operators
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:dword
.data
byte1 BYTE 10,20,30
array1 WORD 30 DUP(?),0,0
array2 WORD 5 DUP(3 DUP(?))
array3 DWORD 1,2,3,4
digitStr BYTE '12345678',0
myArray BYTE 10,20,30,40,50,
60,70,80,90,100
; You can examine the following constant values
; by looking in the listing file (Operator.lst):
;---------------------------------------------
X = LENGTHOF byte1 ;3
X = LENGTHOF array1 ; 30 + 2
X = LENGTHOF array2 ;5*3
X = LENGTHOF array3 ;4
X = LENGTHOF digitStr ; 9
X = LENGTHOF myArray ; 10
X = SIZEOF byte1 ;1*3
X = SIZEOF array1 ; 2 * (30 + 2)
X = SIZEOF array2 ; 2 * (5 * 3)
X = SIZEOF array3 ;4*4
X = SIZEOF digitStr ;1*9
.code
main PROC
invoke ExitProcess,0
main ENDP
END main
Lab Work -3:
As the second example we will see the pointers and TYPEDEF operators. Each
student should run the program and should try to trace it as well.
; Pointers (Pointers.asm)
; Demonstration of pointers and TYPEDEF.
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:dword
; Create user-defined types.
PBYTE TYPEDEF PTR BYTE ; pointer to bytes
PWORD TYPEDEF PTR WORD ; pointer to words
PDWORD TYPEDEF PTR DWORD ; pointer to doublewords
.data
arrayB BYTE 10h,20h,30h
arrayW WORD 1,2,3
arrayD DWORD 4,5,6
; Create some pointer variables.
ptr1 PBYTE arrayB
ptr2 PWORD arrayW
ptr3 PDWORD arrayD
.code
main PROC
; Use the pointers to access data.
mov esi,ptr1
mov al,[esi] ; 10h
mov esi,ptr2
mov ax,[esi] ;1
mov esi,ptr3
mov eax,[esi] ;4
invoke ExitProcess,0
main ENDP
END main
Lab Work -4:
As the second example we will see the sum of array. Each student should run the
program and should try to trace it as well.
; Summing an Array (SumArray.asm)
; This program sums an array of words.
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:dword
.data
intarray DWORD 10000h,20000h,30000h,40000h
.code
main proc
mov edi,OFFSET intarray ; 1: EDI = address of intarray
mov ecx,LENGTHOF intarray ; 2: initialize loop counter
mov eax,0 ; 3: sum = 0
L1: ; 4: mark beginning of loop
add eax,[edi] ; 5: add an integer
add edi,TYPE intarray ; 6: point to next element
loop L1 ; 7: repeat until ECX = 0
invoke ExitProcess,0
main endp
end main
H.W. 2 (deadline: one week)
Write a program with a loop and indexed addressing that calculates the sum of all
the gaps between successive array elements. The array elements are doublewords,
sequenced in nondecreasing order. So, for example, the array {0, 2, 5, 9, 10} has
gaps of 2, 3, 4, and 1, whose sum equals 10.