CEA201 - Chapter 13_3 - Assembly Language_01
CEA201 - Chapter 13_3 - Assembly Language_01
; -------------------------------
end start ; Tell MASM where the program ends
Run a program using the
Menu File/Cmd Prompt
The command
dir *.exe will
show all exe
files stored in
the current
folder.
Run an
application by
it’s file name
(.exe can be
ignored)
EX02_ProcDemo.asm
Procedures are a fundamental building block of programs that are
build directly into the processor using CALL and RET instructions.
This shows how simple it is to do in MASM.
include \masm32\include\windows.inc ; always first start: ; The CODE entry point to the program
include \masm32\macros\macros.asm call main ; branch to the "main" procedure
; ----------------------------------------------------------------- exit
; include files for function calls ; «««««««««««««««««««««««««««««
; -----------------------------------------------------------------
include \masm32\include\masm32.inc main proc
include \masm32\include\gdi32.inc print chr$("Hi, I am in the 'main' procedure",13,10)
include \masm32\include\user32.inc ret ; return to the next instruction after
include \masm32\include\kernel32.inc "call"
; ------------------------------------------------ main endp
; Library files that have definitions for function
; exports and tested reliable prebuilt code. ; «««««««««««««««««««««««««
; ------------------------------------------------
includelib \masm32\lib\masm32.lib end start ; Tell MASM where the program
includelib \masm32\lib\gdi32.lib ends
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
Comments in MASM
Comments are ignored by the assembler
; Comment line
COMMENT delimiter
[Comment block, extending to the closing delimiter]
delimiter
Code
EX03_Data.asm
Print a string declared in the program using the operator OFFSET.
The OFFSET operator tells MASM that the text data is at an OFFSET within
the file which means in this instance that it is in the .DATA section.
Data are declared in the
.data are called as global
data
Basic Data Types in MASM32
Type Abbr Size Integer range Types Allowed
(bytes)
BYTE DB 1 -128.. 127 Character, string
DWORD DD 4 -2Gig..(4Gig-1) 16-bit far per, 32-bit near ptr, 32-bit long word
.data
txtmsg db "I am data in the initialised data section",0
(Source code)
.model flat, stdcall ; 32 bit memory model
option casemap :none ; case sensitive
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
LOCAL txtinput:DWORD ; a "handle" for the text returned by "input"
mov txtinput, input("Type some text at the cursor : ") ; get input string
invoke show_text, txtinput ; show inputted string
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
show_text proc string:DWORD
print chr$("This is what you typed at the cursor",13,10," *** ")
print string ; show the string at the console
print chr$(" ***",13,10)
ret
show_text endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start ; Tell MASM where the program ends
Intel CPU 32-bit Registers
64-bit Lower 32 Lower 16 Lower 8
Intel CPU register bits bits bits
Registers rax
rbx
eax
ebx
ax
bx
al
bl
rcx ecx cx cl
rdx edx dx dl
rsi esi si sil
rdi edi di dil
rbp ebp bp bpl
CS Code Segment rsp esp sp spl
DS: Data Segment r8 r8d r8w r8b
SS: Stack Segment r9 r9d r9w r9b
r10 r10d r10w r10b
r11 r11d r11w r11b
r12 r12d r12w r12b
r13 r13d r13w r13b
r14 r14d r14w r14b
r15 r15d r15w r15b
https://msdn.microsoft.com/en-us/library/windows/hardware/ff561499(v=vs.85).aspx
EX05-Numbers.asm
(1) How to receive numbers from user?
Raw data from keyboard are string. The function sval(string) will convert num-
string to signed number.
(2) How to perform a simple addition using registers
add reg1, reg2 will accumulate value in reg2 to reg1
(3) How to print value in a register/variable to screen
Function str$(number) → num-string
(4) How to compare a memory variable to an immediate number
Use the instruction CMP reg, reg/ CMP reg, var/ CMP var, reg/ CMP mem,
immed/ CMP reg, immed (immed= immediate value)
(5) How to branching to different labels after camparation? Instruction Syntax
Use jumps: JE (equal), JG (greater than), JL (less than)
EX05-Numbrers.asm
Variables Declarations, Input data, Converting data types
EX05-Numbers.asm
Comparing and Branching
EX05-Numbers.asm
; EX05_Numbers.asm
; Declare program model and all libraries using only one file
Source code
include \masm32\include\masm32rt.inc
.code
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
LOCAL var1:DWORD ; 2 DWORD integral variables
LOCAL var2:DWORD ;
LOCAL str1:DWORD ; a string handle for the input data
; compare 2 variables and process the result
; test the MOV and ADD instructions
mov eax, var1 ; copy var1 to eax
print chr$("Add 2 registers: 100 + 250= ")
cmp eax, var2 ; CMP REG, VAR mov eax, 100 ; copy the IMMEDIATE number 100 into the EAX register
je equal ; jump if var1 is equal to 100 to "equal" mov ecx, 250 ; copy the IMMEDIATE number 250 into the ECX register
jg bigger ; jump if var1 is greater than 100 to "bigger" add ecx, eax ; ADD EAX to ECX
jl smaller ; jump if var1 is less than 100 to "smaller" print str$(ecx) ; show the result at the console
print chr$(13,10,13,10) ; 2 empty lines
bigger:
print chr$("The number 1 you entered is greater than number 2",13,10)
jmp over
smaller:
print chr$("The number 1 you entered is smaller than number 2",13,10)
over:
ret
main endp
; «««««««««««««««««««««««««««««««««««««««««««««
Ex2:
Write a MASM
program that will print
the following cantor of
Hàn Mặc Tử
Exercises
Ex3: Write a program that will accept 3 numbers, then
sum of them and their average will be printed out.
◼ 1- Arithmetic operations
◼ 2- Access variable’s address and memory map
◼ 3- Procedure with pointer parameters
◼ 4- Use Loops
1- Arithmetic Operations
EX06_Adding.asm
Write a MASM program that will accept 2 integers, then sum of
them will be print out.
EX06_Adding.asm - Source code
; EX06_Adding.asm Accept 2 integers, sum of them will be printed out
include \masm32\include\masm32rt.inc
sum PROTO :DWORD, :DWORD ; prototype a method + 2 parameters
.code
start: ; The CODE entry point of the program
call main ; branch to the "main" procedure
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc
LOCAL var1:DWORD ; 2 DWORD integral variables
LOCAL var2:DWORD ;
LOCAL result:DWORD ; Result of operation
; Input 2 integers
mov var1, sval(input("Enter number 1 : "))
mov var2, sval(input("Enter number 2 : "))
; Invoke the procedure SUM to compute their sum, result in EAX
push eax ; store EAX to STACK
invoke sum, var1 , var2
mov result, eax ; result = EAX
pop eax ; restore EAX from STACK
EX06_Adding.asm - Source code
ret
main endp
; ««««««««««««««««««««««««««««
sum proc v1: DWORD, v2:DWORD
mov eax, v1 ; eax= v1
add eax, v2 ; eax = eax + v2 -> Result in eax
ret
sum endp
end start
+ 2- Access Variable’s Address
I love you
(txt1)4206596
(anInt)4206592 123
(var1)1245112 1000
Stack segment
(local variables)
Addresses.asm- Access Variable’s Address
+
(txt2)4206964
(aReal)4206960 5809
Data segment
(global variables) I love you
(txt1)4206596
(anInt)4206592 123
(var1)1245112 1000
Stack segment
(local variables)
Addresses.asm- Access Variable’s Address
+
Addresses.asm- Source code
+
; Addresses.asm
; Draw memory of a program
; Access address and value of aReal
include \masm32\include\masm32rt.inc print chr$("Address of aReal:")
.data ; initialized data mov eax, OFFSET aReal
anInt DD 123 print str$(eax)
txt1 db "I love you", 0 print chr$(", value:")
.data? ; Un-initialized data mov aReal, 5809
aReal DD ? print str$(aReal)
txt2 db 128 dup (?) print chr$(13,10)
.code ; Access address and value of txt2
start: ; The CODE entry point to the program print chr$("Address of txt2:")
call main ; branch to the "main" procedure mov eax, OFFSET txt2
exit print str$(eax)
; «««« print chr$(", value:")
main proc print OFFSET txt2
LOCAL var1: DWORD print chr$(13,10)
; Access address and value of local var var1
; Access address and value of anInt mov var1, 1000
print chr$("Address of anInt:") print chr$("Address of var1:")
mov eax, OFFSET anInt ; Operator OFFSET will get address of a global var lea eax, var1 ; LEA get local variable address
print str$(eax) print str$(eax)
print chr$(", value:") print chr$(", value:")
print str$(anInt) print str$(var1)
print chr$(13,10) print chr$(13,10)
; Access address and value of txt1 ret
print chr$("Address of txt1:") main endp
mov eax, OFFSET txt1 ; ««««««««««««««««««
print str$(eax) end start
print chr$(", value:")
print OFFSET txt1
print chr$(13,10)
+ Swap1.asm
◼ This program depicts passing values to arguments when
calling a procedure
◼ Program that will accept 2 integers, swap them then print out
results.
Comment:
This program can
not swap 2 values
Swap1.asm – Source code
+
Swap1.asm – Memory Map when the procedure
+ Swap1 is called
From main
call Stack
1245112 var1: 123 Stack of
main
1245104 var2: 456
Develop a program that will print out the nth element of the
Fibonacci sequence.
Index 1 2 3 4 5 6 7 8 9 10
Fib. 1 1 2 3 5 8 13 21 34 55
If (n = 1 or n =2) eax=1
Else
t1=1; t2= 1
loop n-2 times
eax = t1 + t2
t1= t2
t2 = eax
eax contains the result
Exercises
Develop a procedure for computing n! using the following
prototype:
factorial PROTO : DWORD
◼ Arithmetic operations
◼ Using loops