Lecture3 Assembly
Lecture3 Assembly
Topics
8086 Assembly Language
Programming Evironments
Option1 : MASM Assembler and DOSBox Emulator
Option2 : EMU8086 Emulator/Assembler
Option3 : Visual Studio Code and MASM/TASM Extension
Instruction Format
2
Instruction Set Architecture (ISA) Categories
Depending on the CPU architecture, an Assembly language instruction set is
defined as followings.
Machine Language
Every microprocessor and microcontroller has its own set of Machine language,
and corresponding Assembly language instruction set.
4
Assembly Language
Assembly Language is a symbolic language to represent
the low level binary machine codes.
In Assembly language, mnemonics (short instruction names) are used to
represent the instructions.
Example Assembly language instruction : MOV AL, 5
6
Assembler and Debugger
.code
Basla PROC
Basla ENDP
END Basla
9
Alternative Template2
;CS (Code Segment) register uses logical segment named komutlar.
;DS (Data Segment) register uses logical segment named veriler.
assume ds : veriler , cs : komutlar
veriler segment
veriler ends
komutlar segment
Basla :
komutlar ends
end Basla
10
Example1 : Displaying Text on Screen
(Using BIOS interrupt service 10h)
●
Program below calls BIOS interrupt service, for displaying characters on screen.
●
10h is a software interrupt for video services of BIOS.
●
0Eh is function code for screen displaying.
●
Function parameter is AL register, containing the ASCII code of one character.
.model small
.code
Basla PROC
.EXIT
Basla ENDP
END Basla
11
.MODEL SMALL
.DATA
Cumle DB 'Hello world', 13, 10, '$'
;ASCII codes: 13 is Carriage Return
;10 is Line Feed, $ is string end symbol
hello2.asm
File .CODE
Basla PROC
.STARTUP
.EXIT
Basla ENDP
END Basla
12
ASCII Codes Table
(Decimal)
Null Space
NewLine
Return
13
●
Using decimal numbers for each character:
Cumle DB 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 13, 10, 36
●
Using hexadecimal numbers:
Cumle DB 48h, 65h, 6Ch, 6Ch, 6Fh, 20h, 77h, 6Fh, 72h, 6Ch, 64h, 0Dh, 0Ah, 24h
●
Using binary numbers:
Programming Evironments
Option1 : MASM Assembler and DOSBox Emulator
Option2 : EMU8086 Emulator/Assembler
Option3 : Visual Studio Code and MASM/TASM Extension
Instruction Format
15
MASM Assembler/Linker
●
File Editor : A source program consisting of assembly language
statements is created using any text editor (such as Notepad,
Notepad++, or Visual Studio Code editor, etc.)
●
MASM assembler has its own editor program called PWB.EXE (16-bit)
(Programmer’s Work Bench) which is a program with DOS GUI.
Usage of PWB is optional.
●
Assembler : The MASM assembler program reads the source file and
produces an object file, which is a machine-language translation
(binary code) of the source program.
●
Linker : The object file may contain calls to routines (such as operating
system service functions) in an external link library.
●
The linker then copies the needed routines from the link library into the
object file, creates a special header record at the beginning of the
program, and produces an executable program (binary).
16
Phases of Editing, Assembling, Linking
ASM
Editor file Assembler
LIB Linker
files
Other Object and
Library Files
(If any) EXE
file
Executable file
(Contains binary
machine codes)
17
18
Assemblers for
Intel x86 Processors
●
An assembler is a program (similar to compiler) for translating assembly
language source code program into binary object code.
●
There are many assemblers for Intel x86 architecture such as followings.
Supported
Assembler Programs
Operating Systems
Short
Long Name DOS Windows Linux
Name
Microsoft Macro
MASM √ √ -
Assembler
Borland Turbo
TASM √ √ -
Assembler
Netwide
NASM - - √
Assembler
MASM 6.11 √ √ -
ML64 - √ √
20
DOSBox Emulator Program
●
The lecture covers Intel 8086 CPU (16-bit microprocessor).
●
MASM 6.11 assembler is used for programming examples in the lecture.
●
The 64-bit Windows operating system does not support 16-bit applications.
●
Therefore, it is necessary to use an emulator (simulator) program, which allows
compiling and linking 16-bit 8086 assembly programs.
●
The DOSBox Emulator Program can be used for this purpose.
●
All of the following component programs of MASM 6.11 can be
executed in the DOSBox emulator window.
ML.EXE MASM assembler and linker
PWB.EXE Programmer’s Work Bench editor (usage is optional)
CV.EXE Code View debugger (usage is optional)
21
Step 4) By using Windows File Explorer, create an empty new folder named “codes”
in C:\ root directory, that can be used to store Assembly source program files for
testing. You may choose any other folder name.
C:\
│
├─── DOSBox
│
├─── MASM
│
└─── codes
22
Configuration of DOSBox
Step 1) Run DOSBox.exe program. When executed first time, DOSBox creates a
default configuration file, in the following location:
C:\Users\Username\AppData\Local\DOSBox\dosbox-0.74-3.conf
Step 3) Add the following new commands to the end of configuration file.
mount C C:\MASM
set PATH=C:\BIN;C:\BINR;C:\INCLUDE;C:\LIB
mount D C:\codes
D:
●
The first mount command maps physical directory “C:\MASM“,
onto a temporary virtual drive as “C:\”.
●
Set PATH command tells DOSBox searching paths of MASM binary files.
●
The second mount command maps physical directory “C:\codes“,
onto a temporary virtual drive as “D:\”.
23
ML hello.asm /link
4) If there are no syntax errors, an executable file is generated. (Warnings are skipped)
Run the hello.exe program, by entering executable file name in DOSBox window.
hello.exe
24
Using MASM Assembler Program (ML.EXE)
DOSBox Emulator window
Compiling
& Linking
Executing
25
Command-line Options of
MASM Assembler
●
Simple form of assembling and linking:
ML hello.asm /link
26
Listing File (Generated by Assembler)
hello.lst File
Microsoft (R) Macro Assembler Version 6.11 01/01/23 09:00:00
hello.asm Page 1 – 1
.MODEL SMALL
0000 .DATA
0000 48 65 6C 6C 6F 20 Cumle DB 'Hello world', 13, 10, '$'
77 6F 72 6C 64 0D
0A 24
0000 .CODE
0000 Basla PROC
0000 B8 ---- R mov ax, @data
0003 8E D8 mov ds, ax
0005 BA 0000 R mov dx, OFFSET Cumle
0008 B4 09 mov ah, 09h
000A CD 21 int 21h
000C B4 4C mov ah, 4Ch
000E CD 21 int 21h
0010 Basla ENDP
END Basla
Programming Evironments
Option1 : MASM Assembler and DOSBox Emulator
Option2 : EMU8086 Emulator/Assembler
Option3 : Visual Studio Code and MASM/TASM Extension
Instruction Format
29
INSTALLATION
C:\
Step 1) Unzip the zipped file into a temporary folder. │
Step 2) Run the setup.exe program in the temporary folder. │
Emu8086 is installed in the C:\emu8086\ folder. └─── emu8086
30
Using Editor of EMU8086 (emu8086.exe)
●
Create a new file and write an Assembly program in the EMU8086 integrated editor.
●
Or, open an existing source file (hello.asm).
●
Click the Compile button for assembling only, without running.
If there are no syntax errors, click the Run button which is shown in a new window.
●
Also, clicking the Emulate button will start the Emulator, and
instructions can be executed step-by-step (debug mode).
31
Registers
Machine Codes
(ASCII equivalents)
Machine Codes
(Decimal)
32
Addresses of Instructions (physical) Machine Codes (Hexadecimal)
Emu8086 Debug window
(Source codes)
33
SPA: Space
34
Emu8086 Variables window
(in Data Segment)
Emu8086
Flags window
35
Topics
8086 Assembly Language
Programming Evironments
Option1 : MASM Assembler and DOSBox Emulator
Option2 : EMU8086 Emulator/Assembler
Option3 : Visual Studio Code and MASM/TASM Extension
Instruction Format
36
Installing Microsoft Visual Studio Code Editor,
and MASM/TASM Extension for VSCode
Step1) Download and install Microsoft Visual Studio Code editor from internet.
Step2) Open the VSCode program, then click the Extensions button on left.
In the Search box at top, type “MASM/TASM”.
Step3) Visual Studio Code makes an online search in the Visual Studio
marketplace. It may display more than one extensions to choose.
MASM assembler
TASM assembler
●
All of them run in the Visual Studio Code window.
37
39
Topics
8086 Assembly Language
Programming Evironments
Option1 : MASM Assembler and DOSBox Emulator
Option2 : EMU8086 Emulator/Assembler
Option3 : Visual Studio Code and MASM/TASM Extension
Instruction Format
40
8086 Assembly Language
Instruction Format
General Statement Format (Assembly Instruction Syntax):
Operation
(Mnemonic of Operand(s) ; Comment
Opcode)
●
Operation : Defines the action of the statement.
This field contains either an instruction mnemonic or an assembler directive.
●
Mnemonic : Short instruction name.
●
Operands : Lists one or more items on which the instruction or directive operates.
●
Comment : Provides a comment for the programmer.
Comments are for documentation only. They are ignored by the assembler.
41
Examples
Usage Examples :
MOV AL, 50 ;Move mmed ate data 50 to AL reg ster
42
8086 Machine Language
Instruction Format
●
For every Assembly language instruction written by programmer in source file,
the Assembler Program generates the Binary Machine Codes.
●
Instruction Format of Binary Machine Code is from 1 byte to 6 bytes,
depending upon the addressing modes used for instructions.
●
The following is the 8086 Machine Instruction Format, for maximum of 6-byte
instructions.
Operands (source
Operation
and destination) Address Displacement
Immediate Data
of an Operand
(optional)
OpCode D W MOD REG R/M (optional)
43
Byte1 : Operation
●
Opcode stands for Operation Code.
Every Instruction has a unique 6-bit opcode.
For example, opcode for MOV is 100010.
●
D stands for direction.
If D=0, then direction is from register.
If D=1, then direction is to register.
●
W stands for width.
If W=0, then only a byte is transferred (8 bits).
If W=1, then a whole word is transferred (16 bits).
Operation
OpCode D W
1 Byte
44
Byte2 : Operands
●
Second byte of a machine code instruction indicates whether one of the operands is in
memory or both are in registers.
●
This byte contains three fields: MOD, REG, and R / M
Operands
(source and destination)
MOD REG R/M
2 bits 3 bits 3 bits
1 Byte
Abbreviation Field
Mode of Memory
MOD
Addressing
Register selected
REG
(Second Operand)
Register / Memory
R/M
(First Operand)
45
MOD field
in Byte2
●
MOD field is 2-bits.
●
It encodes the Memory Addressing Mode of the instruction.
Bits of MOD
Memory Addressing Mode
field
46
R / M and
MOD fields in Byte2
●
MOD and R / M fields together is calculated by Assembler, based upon the addressing
mode and register being used in it. They define the second operand.
●
If MOD = 11, then addressing is a register to register mode.
●
If MOD = 00, 01, 10 , then addressing is a memory mode.
●
Table below shows how effective address of memory operand gets selected
for different R / M and MOD values.
(Memory Mode with (Memory Mode with (Memory Mode with (Register
no displacement) 8-bit displacement) 16-bit displacement) Mode)
47
REG field
in Byte2
●
REG field is 3-bits and indicates the register selection for the first operand ,
which can be source / destination operand, depending on the Direction bit (D = 0 / 1 ).
●
Table below shows how REG field along with the status of Width bit (W = 0 / 1)
selects the different registers.
48
Examples : Instruction Formats
1-BYTE
1-BYTE
2-BYTE
2-BYTE
3-BYTE
4-BYTE
3-BYTE
4-BYTE
6-BYTE 49
●
Ocode (Operation Code) for MOV instruction is = 100010
●
AL is source operand, therefore Direction bit D = 0
●
Operands (source and destination) are 8-bit, therefore Width bit W=0
●
Addressing mode is Register Mode, therefore MOD = 11
●
Code for AL register (source operand) is , REG = 000
●
Operation is from register to register, and BL is destination operand.
Therefore Register/Memory bits are , R/M = 011
Number of
Machine Code Bytes
Machine Code
Assembly Instruction Bytes for
Generated by the Assembler
(Hexadecimal)
Instruction
CLC 1 F8
MOV BL, 12h 2 B3 12
MOV BX, 1234h 3 BB 34 12
MOV [BX], 1234h 4 C7 07 34 12
MOV [BX]+1234h, 56h 5 C6 87 34 12 56
MOV [BX]+[DI]+[1234h], 5678h 6 C7 81 34 12 78 56
51
.model small
.code
CLC ;1 byte instruction
MOV BL, 12h ;2 bytes instruction
MOV BX, 1234h ;3 bytes instruction
MOV [BX], 1234h ;4 bytes instruction
MOV [BX]+1234h, 56h ;5 bytes instruction
MOV [BX]+[DI]+1234h, 5678h ;6 bytes instruction
.EXIT
END
52