Chapter 6
Assembly (1)
1
Objectives
After studying this part, you should be able to:
› Familiarize yourself with the assembly
language, a low level language
› Understand how a program is compiled
› Develop some basic console applications
Contents
› 1- Install 32/64-bit MASM – MS Macro Assembly
› 2- MASM Integrated Development Environment(IDE)
› 3- Introduction to Microsoft Macro Assembly
Language
› Some sample programs
1- Install 32-bit MSAM
› Microsoft Macro Assembler: an x86 assembler that uses
the Intel syntax for MS-DOS and Microsoft Windows.
Beginning with MASM 8.0 there are two versions of the
assembler - one for 16-bit and 32-bit assembly sources,
and another (ML64) for 64-bit sources only (Wiki)
› Unzip: masm32v11r.zip Install.exe Run for installation
› Interface after installation:
Install 32-bit MSAM…
› Installed Contents:
Desktop Icon of MASM, executable file: qeditor.exe
Compiler: bin/ml.exe (32 bit), ml64.exe (64 bit)
You should create a folder as a storage of your
exercises
2- MASM Integrated Development
Environment
Menu file Menu
allows user Project
working with allows user
files, run compiling
program,… program,…
2- MASM Integrated Development
Environment
Menu Help allows user referencing to relative
topics:
Using editor
Build-in Libraries in MASM
Opcodes of Intel CPU
Syntaxes of MASM language
3- Introduction to MS Assembly
Demo 1: Write The flat memory model is a non-segmented configuration available in 32-bit
operating systems.
an Assembly
program that
displays the
string 'Hello
Directives helps
World' on the
the program will
screen: conform to
Windows
How to
Form of a create it?
MASM NEXT
program SLIDE
EX01_Hello.asm
Step 1: Open MASM/ Menu File/ New
Step 2: Copy and paste the code in the next slide to it’s editor
Step 3: Save file/EX1_Hello.asm
Step 4: Menu Project/ Console Assemble&Link
Step 5: View results in containing folder
Step 6: Run the program: Click the EX01_Hello.exe
A black window will show then disappear because
there is no code to block it.
An Assembly source code is a file whose extension MUST
BE .ASM
What is the result of compilation?
You can see them in the folder containing you ASM files
EX01_Hello.asm - Code
; «« Comment begins with ';' to the end of a line
; From masm32\tutorial\console\demo1
;
; Build this with the "Project" menu using
Results
; "Console Assemble and Link"
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««
.486 ; create 32 bit code
.model flat, stdcall
option casemap :none
; 32 bit memory model
; case sensitive
How to run the program and we can see it?
include \masm32\include\windows.inc ; always first
Create EX01_Hello.bat file the run it.
include \masm32\macros\macros.asm ; MASM support macros
; -----------------------------------------------------------------
; include files that have MASM format prototypes for function calls
; -----------------------------------------------------------------
include \masm32\include\masm32.inc
include \masm32\include\gdi32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
; ------------------------------------------------
; Library files that have definitions for function exports
; and tested reliable prebuilt code.
; ------------------------------------------------
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
.code ; Tell MASM where the code starts
start: ; The CODE entry point to the program
print chr$("Hello world!",13,10) ; 13: carriage return, 10: new line
exit ; exit the program
; -------------------------------
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.
This code is in the directory masm32\tutorial\console\demo2\Proc.asm
Procedure
syntax
EX02_ProcDemo.asm- Source/Run
; From masm32\tutorial\console\demo2
; Build this with the "Project" menu using
; "Console Assemble and Link"
; ««««««««««««««««««««««««««««««««««
.486 ; create 32 .code ; Tell MASM where the
bit code code starts
.model flat, stdcall ; 32 bit
memory model ; ««««««««««««««««««««««««««
option casemap :none ; case
sensitive start: ; The CODE entry point
to the program
include \masm32\include\windows.inc ; always call main ; branch to the "main"
first procedure
include \masm32\macros\macros.asm exit
; ----------------------------------------------------------------- ; «««««««««««««««««««««««««««««
; include files for function calls
; ----------------------------------------------------------------- main proc
include \masm32\include\masm32.inc print chr$("Hi, I am in the 'main' procedure",13,10)
include \masm32\include\gdi32.inc ret ; return to the next
include \masm32\include\user32.inc instruction after "call"
include \masm32\include\kernel32.inc main endp
; ------------------------------------------------
; Library files that have definitions for function ; «««««««««««««««««««««««««
; exports and tested reliable prebuilt code.
; ------------------------------------------------ end start ; Tell MASM where the
includelib \masm32\lib\masm32.lib program ends
includelib \masm32\lib\gdi32.lib
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
WORD DW 2 -32768..32767 16-bit near ptr, 2 characters, double-byte character
DWORD DD 4 -2Gig..(4Gig-1) 16-bit far per, 32-bit near ptr, 32-bit long word
FWORD DF 6 -- 32-bit far ptr
QWORD DQ 8 -- 64-bit long word
D: Defined
TBYTE DT 10 -- BCD, 10-byte binary number
REAL4 DD 4 -- Single-precision floating number
REAL8 DQ 8 -- Double-precision floating number
REAL10 DT 10 -- 10-byte floating point
Initialized data has this form:
.data Uninitialized data has this form:
var1 dd 0 ; 32 bit value initialized to zero .data?
var2 dd 125 ; 32 bit value initialized to 125 udat1 dd ? ; Uninitialized single 32 bit
txt1 db "This is text in MASM",0 ; Initialize a NULL space
string buffa db 128 dup (?) ;
array dd 1,2,3,4,5,6,7,8 ; array of 8 initialized buffer 128 bytes
elements
EX03_Data.asm - Source
; From K:\masm32\tutorial\console\demo3
; Build this with the "Project" menu using
; "Console Assemble and Link"
.486 ; create 32 bit
code
.model flat, stdcall ; 32 bit memory
model
option casemap :none ; case sensitive
include \masm32\include\windows.inc ; always first
include \masm32\macros\macros.asm ; MASM
support macros
include \masm32\include\masm32.inc
include \masm32\include\gdi32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
.data
txtmsg db "I am data in the initialised data section",0
.code ; Tell MASM where the code
starts
; ««««««««««««««««««««««««««««««««««««««««««««««
start: ; The CODE entry point to the
program
call main ; branch to the "main"
procedure
exit
;
«««««««««««««««««««««««««««««««««««««««««««««««««
«
main proc
print OFFSET txtmsg
ret ; return to the next instruction
EX04_Locals.asm
How to use of LOCAL variables declared in a procedure?
When the procedure is called, these variables are allocated in program’s
stack
DECLARE LOCAL VARIABLES
LOCAL MyVar:DWORD ; allocate a 32 bit space on the stack
LOCAL Buffer[128]:BYTE ; allocate 128 BYTEs of space for TEXT
data.
How to PROTOTYPE and implement a procedure along with it’s
parameters?
How to call user-defined procedure?
How can program receive input from user? Build-in function:
input(“warning:”)
EX04_Locals.asm ; Source code From masm32\tutorial\console\demo4\locals.asm
(Source code) .486
.model flat, stdcall
; create 32 bit code
; 32 bit memory model
option casemap :none ; case sensitive
include \masm32\include\windows.inc ; always first
include \masm32\macros\macros.asm ; MASM support macros
include \masm32\include\masm32.inc
include \masm32\include\gdi32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
show_text PROTO :DWORD ; prototype a method + type of parameter
.code ; Tell MASM where the code starts
start: ; The CODE entry point to the program
call main ; branch to the "main" procedure
exit
;
««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
«««««««
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
;
Intel CPU 32-bit Registers
64-bit Lower 32 Lower 16 Lower 8
register bits bits bits
rax eax ax al
rbx ebx bx bl
rcx ecx cx cl
Intel CPU rdx edx dx dl
Registers rsi esi si sil
rdi edi di dil
rbp ebp bp bpl
CS Code Segment
DS: Data Segment rsp esp sp spl
SS: Stack Segment r8 r8d r8w r8b
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
https://msdn.microsoft.com/en- r15d r15w r15b
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
(2) How to print value in a register/variable to screen
Function str$(number) num-string
(3) 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 vakue)
(4) 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
; Declare program model and all libraries using only one file
EX05-Numbers.asm include \masm32\include\masm32rt.inc
Source code .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
; compare 2 variables and process the result LOCAL var2:DWORD ;
LOCAL str1:DWORD ; a string handle for the input data
mov eax, var1 ; copy var1 to eax
cmp eax, var2 ; CMP REG, VAR ; test the MOV and ADD instructions
je equal ; jump if var1 is equal to 100 to "equal"
print chr$("Add 2 registers: 100 + 250= ")
jg bigger ; jump if var1 is greater than 100 tomov eax, 100 ; copy the IMMEDIATE number 100 into
"bigger" the EAX register
mov ecx, 250 ; copy the IMMEDIATE number 250 into
jl smaller ; jump if var1 is less than 100 to "smaller"
the ECX register
add ecx, eax ; ADD EAX to ECX
equal: print str$(ecx) ; show the result at the console
print chr$("2 numbers you entered are equal.",13,10) print chr$(13,10,13,10) ; 2 empty lines
jmp over
; Input 2 integers
mov var1, sval(input("Enter number 1 : "))
bigger: mov var2, sval(input("Enter number 2 : "))
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
; «««««««««««««««««««««««««««««««««««««««««««««
Exercises
Part 1: Write answers to your notebook
Use the Opcodes help of the menu Help, describe syntaxes of following MASM
instructions
(1) ADD
(2) SUB
(3) MUL, IMUL
(4) DIV, IDIV. What register will store the remainder ?
Part 2:
Write a MASM program that
will print the following cantor
of Hàn Mặc Tử
Summary
› Form of a MASM program
› Variable declarations: DB, DD, DW, …
› Basic input, output operations: print, chr$(…), str$(…)
› Data type conversion: sval(..),
› Procedure with parameters: CALL, INVOKE
› Instructions: MOV, ADD, CMP, JE, JG, JL
Chapter 6
Assembly (2)
28
Objectives
After studying this chapter, you should be able to:
› Perform arithmetic operations
› Access variable’s address
› Draw the memory map of a program
› Understand the way procedures work.
› Use a loop operation
Contents
› 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_Sum.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 to 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
; Print the result
print chr$("Sum of them:")
print str$(result)
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
1- Access Variable’s Address
› The following program depicts how to get
addresses of variable:
– The operator OFFSET for global variables
– The instruction LEA for local variables
– Draw memory map of a program
Addresses.asm- Access Variable’s Address
Data segment (txt2)4206964
(global (aReal)420696 5809
variables) 0
I love you
(txt1)4206596
(anInt)4206592 123
(var1)1245112 1000
Stack segment
(local variables)
Addresses.asm- Access Variable’s Address
(txt2)4206964
(aReal)420696 5809
0
Data segment
(global I love you
variables)
(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
print chr$("Address of aReal:")
include \masm32\include\masm32rt.inc
mov eax, OFFSET aReal
.data ; initialized data
print str$(eax)
anInt DD 123
print chr$(", value:")
txt1 db "I love you", 0
mov aReal, 5809
.data? ; Un-initialized data
print str$(aReal)
aReal DD ?
print chr$(13,10)
txt2 db 128 dup (?)
; Access address and value of txt2
.code
print chr$("Address of txt2:")
start: ; The CODE entry point to
mov eax, OFFSET txt2
the program
print str$(eax)
call main ; branch to the "main"
print chr$(", value:")
procedure
print OFFSET txt2
exit
print chr$(13,10)
; ««««
; Access address and value of local var
main proc
var1
LOCAL var1: DWORD
mov var1, 1000
print chr$("Address of var1:")
; Access address and value of anInt
lea eax, var1 ; LEA get local
print chr$("Address of anInt:")
variable address
mov eax, OFFSET anInt ; Operator OFFSET will get
print str$(eax)
address of a global var
print chr$(", value:")
print str$(eax)
print str$(var1)
print chr$(", value:")
print chr$(13,10)
print str$(anInt)
ret
print chr$(13,10)
main endp
; Access address and value of txt1
; ««««««««««««««««««
print chr$("Address of txt1:")
end start
mov eax, OFFSET txt1
print str$(eax)
print chr$(", value:")
print OFFSET txt1
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.
› This version can not perform requirements successfully
because the procedure swap1 accesses variable directly
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
124510 var2: 456
4
124508 v1: 123 Stack of
8 swap1
124508 v2: 456
4
Copy var1 v1 Procedure swap1
Copy var2 v2
All statements do
can not modify
not relate to var1. values of
var2 arguments
Swap2.asm
› This program will repair the failure of the previous program
in which a procedure is declared using pointers ( address of
data)
1245112 (var2) var1: 456 Stack
of
1245104 (var1) var1: 456 main
1245088 1245104 Stack
(add1) of
1245084 1245112 swap2
(add2)
t1= value at add1
To change values of t2= value at add2
var1 and var2 Value at add1 = t2
Value at add2= t1
Swap2.asm – Source code
Swap2.asm – Source code
1245112 (var2) var1: 456 Stack
of
1245104 (var1) var1: 456 main
Swap2.asm – Source code
1245112 (var2) var1: 456 Stack
of
1245104 (var1) var1: 456 main
1245088 1245104 Stack
(add1) of
1245084 1245112 swap2
(add2)
t1= value at add1
To change values of t2= value at add2
var1 and var2 Value at add1 = t2
Value at add2= t1
Loop.asm
The following program depicts a way to use loop.
Program will perform 3 times, for each time, 2 integers will be accepted then
sum of them is print out.
Loop.asm – Source code
Loop.asm
– Source
code
Exercises
Develop a MASM program that will accept 2 integers then print out their sum,
difference, product, quotient and remainder. (Refer to the EX06_sum.asm as a
sample)
Develop a MASM program that will accept 3 integers then print out their sum.
Develop a program that will perform n times, for each time, 2 integers will be
accepted then sum of them is print out. Value of n is received from user.
Summary
› Arithmetic operations
› Accessing variable’s address
› Drawing the memory map of a program
› The way procedures work.
› Using loops