Lab Set I (New)
Lab Set I (New)
Lab Set I (New)
Lab Set I
Assembling, Link, And Run an Assembly Program
Objective: Introduce tools and how to use them to assemble link and run
assembly programs.
Required knowledge, skill and materials:
● Knowledge of command-line Interface usage is required, basics commands to change directory and
run commands.
● Software
o MASM32 (Windows 16 bit & 32-bit assembler), NASM (Linux 16, 32 & 64-bit assembler)
are assemblers. If you already have VS code, installing MASM / TASM extension.
o Visual Studio (For 64-bit MASM, VS Express or VS Community is fine).
o Debug.exe: Helps you to disassemble or debug 16-bit or 32-bit binary files (In 32-bit
system you can find it inside system32 folder)
o DOS Box: is required if you want to run Debug.exe and 16-bit real mode executables on 64
bit windows system because 64-bit windows doesn’t support 16-bit real mode applications
anymore.
o Emu8086, 8086 emulator for 32 bit and 64 bit environment
Grading:
i. Attend the lab session and demonstrate your work. You must sign attendance sheet.
ii. Write a brief report and submit HERE addressing those questions labeled with “Q” .
Introduction:
8086 is a 16-bit processor with 16-bit registers. Various processors with 32-bit or 64-bit capabilities that share
the same architecture as the 8086 are known as the x86 family. There are two main categories of processor
usage: the first is in personal computer lines, where processors are used under operating system control, such
as in desktops, laptops, and handheld devices. The second category is in microcontrollers, typically used for
embedded system purposes. In this lab series, the focus will be on the first usage, where you'll need to create
executables in a format that the underlying operating system can understand (e.g., EXE files in Windows).
Subsequent labs will cover programming microcontrollers and interfacing with sensors.
Before you start, make sure that you have installed MASM32 or equivalent assembler.
1. Copy Eg.asm code bellow into a new notepad or notepad++ and save it in your working directory
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
a. You can make “C:\masm32\bin” (if you have installed MASM32 in ‘C’) your working
directory by running cd /D c:\masm32\bin.
b. Or make sure to append “path” environment variable with “c:\masm32\bin” (location of
“ml.exe”) by running set PATH=%PATH%;C:\masm32\bin and continue to working in your
default working directory.
2. Run this command ml /c Eg.asm to assemble without linking (/c)
3. Run this command link16 Eg.obj to link and create executable
4. Now you have a 16-bit executable program (Eg.exe); unfortunately, it only runs on 16 or 32-bit OS
but not on 64-bit OS; hence, you need to use emulator for 16-bit real mode executable such as
DOSBox.
//Eg.asm
// *******************************************************
;Hello World example
.MODEL SMALL
.DATA
msg db "Hello, World!$"
.CODE
MAIN PROC FAR
mov ax,seg msg
mov ds,ax
mov dx, offset msg
mov ah, 9 ;
int 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
// *******************************************************
2
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
‘A’ was expected instead of ‘7’. Note that 65 and 55 are ASCII codes of characters ‘A’ and ‘7’
respectively. What method is used to insure that downloaded programs are not modified
unauthorized, as you did?
8. Now, type ‘?’ and play around with commands.
// Eg2.asm
// *******************************************************
;Eg.asm
.MODEL SMALL
.STACK 64
;
.DATA
DATA1 DB 60
DATA2 DB 5
SUM DB ?
;
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
MOV AL,DATA1
MOV BL,DATA2
SUB AL,BL
MOV AH,02
MOV DL,AL
INT 21H
MOV SUM,AL
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
// *******************************************************
3
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
Now, you will use emu8086 which simulates the 8086 processors and has built-in assembler and debugger on
top. Read Appendix C
1. Open emu8086.exe
2. From the file -> examples menu, open stepper motor. Note that this assembly code uses a
virtual stepper motor connected to virtual port 7
3. Click on emulate and observe the stepper motor.
4. Then click on single step to go through the code line by line (debug) and understand how it
works.
Q What is the step angle of the motor in half and in full step mode.
Q Modify the code so that the stepper motor rotates clockwise by 90⁰ whenever you press the up-
arrow key and the reverse when you press the down-arrow key.
Hint: use mov ah,0
int 16h
to get scan code for up and down keys and cmp instruction to choose among two directions. Note that
4
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
1. Open Visual Studio, Create a New empty c++ Project and name it MASM
2. Under solution explorer, right click on MASM and choose “Build Dependencies-> Build
Customizations and select masm(.target, .props).
3. Right click on MASM again and choose Properties, then select Link-> System->SubSystem choose
WINDOWS (/Subsystem:Windows)
4. Go to Configuration Properties -> Linker -> Advanced. In Advanced at the top should be Entry Point.
Type in main
5. Right click on MASM again Add->new Item->c++ File and give it a name as eg4.asm
6. Now copy and paste the following code. Right click at main PROC and select Run to Cursor to start
debugging.
7. Now from Debug->Windows open Registers, Memory and Disassembly windows to check the actual
register values and memory content of your computer then continue to debug line by line.
Q From the disassembly window, how many memory space does the “MOV eax,DATA1” instruction use
(optinal)
8. Open File->open->file open Eg.exe (of previous exercise) and try to modify to display your name
instead of “Hello World”. Never try to edit working executable in such away unless
Eg4.asm
// *******************************************************
.386
.model flat, stdcall
.stack 4096
.data
DATA1 DD 52H
DATA2 DD 29H
SUM DD ?
.code
main PROC
MOV eax,DATA1
MOV ebx, DATA2
SUB eax,ebx
MOV SUM,eax
main endp
end
// *******************************************************
b. Building a 64-bit assembly program
1. Follow the same procedure 1 to 4 in (a), and copy the following code (Eg5.asm) instead of
Eg4.asm
2. Change programing environment from x86 to x64 from drop down menu
5
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
3. Now copy and paste the following code. Right click at main PROC and select Run to Cursor to start
debugging.
Eg5.asm
// *******************************************************
.DATA
DATA1 DQ 52H
DATA2 DQ 29H
SUM DQ ?
.CODE
main PROC
MOV rax,DATA1
MOV rbx, DATA2
SUB rax,rbx
MOV SUM,rax
main endp
end
// *******************************************************
Q What is the basic difference between the codes in Eg4.asm and Eg5.asm?
6
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
7
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
You can see that a 64-bit processor supports smaller register addressing by providing a mechanism to
address portion of its 64 bit registers. For example the accumulator register ‘A’ has different prefix,
RAX, EAX, AX, AH, AL, to address the whole 64 bit, 32 bit, 16 bit, higher 8 bit or lower 8 bit
respectively.
The operating mode of an OS determines whether it can run 16-bit real mode program or not. Even
though 16-bit real mode program cannot be run on 64-bit OS which doesn’t support real mode, a 64-
bit processor still supports real mode. BIOS systems run in real mode (legacy mode) before they load
OS and elevate the mode by setting the "Protected Mode Enable" flag (PE) inside control register
(CR0). SO how can your run real mode programs?
8
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
You might wonder at this point why to stick to the old 16-bit real mode if you can write a 32-bit or 64
bit protected mod program that can run on 64-bit system; the reason is:
we are working on native 8086 assembly language for educational purpose and using Link16
which generates 16-bit real mode executable.
There are 16-bit microcontrollers working in this mode
BIOS system have components programed in this mode.
Real mode
This is the legacy 8086 segmented memory model. Segments can be up to 64 KB in size (16-bit
addresses within a segment) and the maximum address space is limited to 220 bytes (1 MB). This is the
mode used by the PC BIOS. This the what 8086 uses.
Huge huge far multiple code and data segments; single array may be >64 KB
* In the Tiny model, all four segment registers point to the same segment.
** In all models with near data pointers, SS equals DS.
*** Stack is always limited to at most 64 KByte.
9
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
selector and the offset (address within the segment). While certain segments are associated with specific
operations (e.g., code, stack), the processor supports 16,383 segments, each of which can address up to
232 bytes (4 GB). A full memory address, called a far pointer is a 16-bit segment descriptor and a 32-
bit offset within the segment.
In this addressing mode, the processor maintains base address for the descriptor table in two of its
registers called GDTR and LDTR and uses segment selector as offset address and calculates the
physical address of the selected segment descriptor. Content of the selector segment descriptor will be
copied and maintained in hidden part of the segment registers. Descriptors are created by compilers,
linkers, loaders, or the operating system, not by applications programmers. Once the descriptor is found,
its base address is added to the offset address for example from IP register to get the linear address.
A segment descriptor table is an array of 8-byte (64 bit) segment descriptors saved in the memory.
10
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
The x86-64 architecture disables segmentation in long mode (64-bit mode) creating a flat 64-bit linear-
address space; in this mode, CS, SS, DS, and ES are forced to 0, and the segment limit to 264. x86-
64 architecture maintains all the segment registers for use of the compatible mode and of course it
routinely uses FS and GS for special purposes. The primary purpose of segment registers was to get
around address space limitation in a 16-bit DOS world to address more address space than your registers
can in flat model. Now in 64 bit, since you can address large RAM size (practically 256TB extendable
up to 4 EB which is equivalent to all words ever spoken by human being) you don’t need segment
registers for their original purposed. However, the protection associated with segmentation will not be
there.
Memory Paging
The 4 GB linear address space is divided into 1048576 pages of 4 kB (also supports 4 MB page size).
Usually virtual memory is much larger than implemented one. When accessing memory page that is
not in physical memory an exception is generated. The exception allows the supervisor software to load
required page (e.g. from HDD) transparently to the user program. The “new” page is loaded into
physical memory in place of some other page which must be written on HDD. During such operation
address translation is required. To make paging fast, buffer and cache memory are usually used.
.
11
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
Half-stepping
During half-stepping, the drive alternates between two phases on and a single phase on. This
increases the angular resolution.
Full-stepping
During full-step driving two phases are always on so the motor will provide its maximum rated
torque
12
AAiT, SECE
Microcomputers and Interfacing - Lab Set I - Draft III- Kinde M. 2024
.model flat,stdcall
option casemap:none
include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\user32.inc
includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\user32.lib
.data
.code
main:
invoke MessageBox, NULL, addr msg, addr Caption, MB_OK
invoke ExitProcess, NULL
end main
// *******************************************************
13