Assmebly 8086
Assmebly 8086
This processor formed the basis for IBM's first personal computer, launching the PC revolution.
Today's complex Intel and AMD processors still maintain compatibility with the original 8086
instruction set, making it an excellent starting point for learning assembly language.
What is EMU8086?
EMU8086 is an emulator and integrated development environment (IDE) for the 8086
microprocessor. It allows you to:
Installing EMU8086
1. Download EMU8086 from an official source
2. Run the installer and follow the installation wizard
3. If prompted for registration, you can use:
• User: ISHAAN,glaitm
• Key: 27R3VDEFYFX4N0VC3FRTQZX
1. Quick Start Tutor: Provides documentation, tutorials, and references for the instruction set
and interrupts
2. Code Examples: Pre-written assembly programs to study and modify
3. Recent Files: Quick access to recently opened .asm files
4. New: Create a new assembly program with different templates
• Code Editor: The main area where you write your assembly code, with syntax highlighting
• Menu and Toolbar: Access to commands like assemble, run, and debug
• Registers Window: Displays current values of all CPU registers
• Flags Window: Shows the status of processor flags (Zero, Carry, Sign, etc.)
• Memory Viewer: Allows you to inspect and modify RAM contents
• Stack Window: Shows the contents of the stack segment
4. Click the "Emulate" button (or press F5) to assemble and run the program
5. The emulator will open, showing registers and memory
6. Click "Single Step" repeatedly to execute the program line by line
7. Watch how the register values change with each instruction
Segment Registers
• CS: Code Segment - points to the segment containing the current code
• DS: Data Segment - points to the segment containing global variables
• SS: Stack Segment - points to the segment containing the stack
• ES: Extra Segment - an additional segment register for string operations
Special Registers
• IP: Instruction Pointer - points to the next instruction to execute (offset in the code segment)
• FLAGS: Contains status and control flags that reflect the result of operations and control CPU
behavior
Memory Model
The 8086 uses a segmented memory model with a total addressable memory of 1MB (2^20
bytes). Memory is divided into segments, with each segment having a maximum size of 64KB
(2^16 bytes).
In EMU8086, the most common memory model used is the "small" model, which uses one code
segment and one data segment.
Directives
Directives are instructions to the assembler rather than to the processor. They don't generate
machine code but guide the assembly process.
• ORG: Sets the starting address for the code (ORG 100h for COM programs)
• .MODEL: Specifies the memory model (small, medium, large, etc.)
• .STACK: Defines the stack size
• .DATA: Marks the beginning of the data segment
• .CODE: Marks the beginning of the code segment
• DB/DW/DD: Defines byte, word, or double word data
• PROC/ENDP: Marks procedure boundaries
• END: Marks the end of the source file
Data Declaration
You can define variables and constants using these directives:
; Byte declarations
my_byte DB 42 ; Single byte with value 42
my_char DB 'A' ; Single character
my_string DB 'Hello' ; String of characters
bytes DB 1, 2, 3, 4 ; Array of bytes
; Uninitialized data
buffer DB 100 DUP(?) ; Reserves 100 bytes without initialization
Basic Instructions
The 8086 instruction set has about 116 instructions. Let's cover some fundamental ones:
Data Movement
• MOV: Copies data from source to destination
Arithmetic Operations
• ADD: Addition
ADD AX, BX ; AX = AX + BX
ADD CX, 5 ; CX = CX + 5
• SUB: Subtraction
SUB AX, BX ; AX = AX - BX
SUB DX, 10 ; DX = DX - 10
MOV AX, 5
MOV BX, 3
MUL BX ; AX = AX * BX (result in AX or DX:AX)
• DIV: Unsigned division
MOV AX, 10
MOV BL, 3
DIV BL ; AL = AX / BL, AH = remainder
Logical Operations
• AND: Bitwise AND
• OR: Bitwise OR
OR AX, BX ; AX = AX OR BX
OR DL, 80h ; Set highest bit of DL
Control Flow
• JMP: Unconditional jump
Stack Operations
• PUSH: Push value onto stack
String Operations
• MOVSB/MOVSW: Move string byte/word
• CMPSB/CMPSW: Compare string byte/word
• STOSB/STOSW: Store AL/AX to string
• LODSB/LODSW: Load string byte/word into AL/AX
• SCASB/SCASW: Scan string for byte/word
Addressing Modes
The 8086 supports various ways to specify operands:
ORG 100h
sum_loop:
ADD AL, [SI] ; Add current element to sum
INC SI ; Move to next element
LOOP sum_loop ; Decrement CX and loop if CX != 0
; Result is now in AL
RET
Example 2: Converting Lowercase to Uppercase
ORG 100h
; Define a string
message DB 'Hello, World!', 0 ; 0 marks the end of string
; Convert to uppercase
MOV SI, OFFSET message ; SI points to the string
convert_loop:
MOV AL, [SI] ; Get current character
CMP AL, 0 ; Check if end of string
JE done ; If yes, we're done
next_char:
INC SI ; Move to next character
JMP convert_loop ; Repeat for next character
done:
RET
max_loop:
MOV AL, [SI] ; Get current element
CMP AL, BL ; Compare with current max
JLE not_greater ; Jump if not greater
MOV BL, AL ; Update max if greater
not_greater:
INC SI ; Move to next element
LOOP max_loop ; Repeat until done
ORG 100h
; Display a message
MOV AH, 9 ; DOS function: print string
MOV DX, OFFSET message ; DX points to the message
INT 21h ; Call DOS interrupt
RET
; Display prompt
MOV AH, 9 ; DOS function: print string
MOV DX, OFFSET prompt ; DX points to the prompt
INT 21h ; Call DOS interrupt
; Read a character
MOV AH, 1 ; DOS function: read character
INT 21h ; AL now contains the character
; Display newline
MOV AH, 9
MOV DX, OFFSET newline
INT 21h
; Display message
MOV AH, 9
MOV DX, OFFSET message
INT 21h
RET
Procedures
Procedures allow you to organize code into reusable blocks:
ORG 100h
; Main program
CALL print_message ; Call the procedure
RET ; Return to OS
; Procedure definition
print_message PROC
MOV AH, 9 ; DOS function: print string
MOV DX, OFFSET message ; DX points to the message
INT 21h ; Call DOS interrupt
RET ; Return to caller
print_message ENDP
Macros
Macros are like inline functions that get expanded at assembly time:
ORG 100h
Conditional Assembly
The assembler can include or exclude code based on conditions:
DEBUG EQU 1 ; Set DEBUG flag (1 = on, 0 = off)
ORG 100h
MOV AX, 5
MOV BX, 3
ADD AX, BX ; AX = 8
IF DEBUG
; Debug code only assembled if DEBUG is non-zero
MOV AH, 9
MOV DX, OFFSET debug_msg
INT 21h
ENDIF
RET
Memory Management
Understanding memory organization is crucial for effective assembly programming:
.MODEL SMALL ; Small memory model
.STACK 100h ; 256 bytes for stack
; Return to OS
MOV AX, 4C00h ; DOS function: exit program
INT 21h ; Call DOS
MAIN ENDP
Step-by-Step Execution
1. Assemble your program by clicking the "Emulate" button
2. Use the "Single Step" button (or F8) to execute one instruction at a time
3. Watch register and memory changes after each instruction
4. Use the "Back" button to step backward if needed
Breakpoints
1. Set breakpoints by clicking on the margin next to instructions
2. Click "Run" to execute up to the breakpoint
3. Continue debugging with "Single Step" or "Run"
Flags Analysis
The flags window shows the status of processor flags:
Understanding how instructions affect these flags helps debug conditional jumps and other
operations.
Screen Output
The simplest way to display output is through DOS interrupts:
ORG 100h
; Display a character
MOV AH, 2 ; DOS function: display character
MOV DL, 'A' ; Character to display
INT 21h ; Call DOS interrupt
; Display a string
MOV AH, 9 ; DOS function: display string
MOV DX, OFFSET message
INT 21h ; Call DOS interrupt
RET
ORG 100h
; Read a character
MOV AH, 1 ; DOS function: read character
INT 21h ; Character is returned in AL
; Read a string
MOV AH, 0Ah ; DOS function: buffered input
MOV DX, OFFSET buffer
INT 21h ; Read string into buffer
RET
Graphical Display
EMU8086 can access the video memory for graphics:
ORG 100h
; Draw a pixel
MOV AH, 0Ch ; BIOS function: draw pixel
MOV AL, 4 ; Color (4 = red)
MOV CX, 100 ; X-coordinate
MOV DX, 100 ; Y-coordinate
INT 10h ; Call BIOS interrupt
RET
• Robot: A simulated robot that can move and interact with its environment
• Stepper Motor: A motor simulation showing rotation
• LED Display: A display of LEDs that can be turned on or off
• Traffic Lights: A simulated traffic intersection
You can access these devices by selecting them from the "Virtual Devices" menu in EMU8086.
BIOS Interrupts
• INT 10h: Video services
◦ Function 0 (AH=0): Set video mode
◦ Function 0Eh (AH=0Eh): Write character in teletype mode
◦ Function 0Ch (AH=0Ch): Write pixel
• INT 16h: Keyboard services
◦ Function 0 (AH=0): Read keyboard
◦ Function 1 (AH=1): Check for keystroke
• INT 1Ah: Time services
◦ Function 0 (AH=0): Get system time
; Get operator
MOV AH, 1 ; Read character
INT 21h
MOV CL, AL ; Store operator in CL
do_add:
MOV AX, CX
ADD AX, BX
JMP display_result
do_sub:
MOV AX, CX
SUB AX, BX
JMP display_result
do_mul:
MOV AX, CX
MUL BX ; Result in AX (or DX:AX if large)
JMP display_result
do_div:
MOV AX, CX
MOV CX, BX ; Save divisor in CX
MOV DX, 0 ; Clear DX for division
DIV CX ; AX = DX:AX / CX, remainder in DX
JMP display_result
invalid_op:
MOV AH, 9
MOV DX, OFFSET invalid_msg
INT 21h
JMP done
display_result:
; Display result message
MOV BX, AX ; Save result in BX
MOV AH, 9
MOV DX, OFFSET result_msg
INT 21h
done:
RET
invalid_digit:
MOV AH, 9
MOV DX, OFFSET invalid_digit_msg
INT 21h
end_read:
MOV AX, BX ; Return result in AX
POP CX
POP BX
RET
get_number ENDP
; Handle 0 specially
CMP AX, 0
JNE convert_loop
MOV AH, 2
MOV DL, '0'
INT 21h
JMP end_display
convert_loop:
CMP AX, 0
JE display_loop
MOV DX, 0
DIV BX ; DX:AX / 10, quotient in AX, remainder in DX
PUSH DX ; Save remainder (digit)
INC CX ; Increment digit counter
JMP convert_loop
display_loop:
CMP CX, 0
JE end_display
POP DX ; Get digit
ADD DL, '0' ; Convert to ASCII
MOV AH, 2
INT 21h ; Display digit
DEC CX
JMP display_loop
end_display:
POP DX
POP CX
POP BX
POP AX
RET
display_number ENDP
This calculator reads two numbers and an operator, performs the calculation, and displays the
result. It demonstrates input/output operations, arithmetic, procedures, and error handling.
Project 2: String Manipulation Toolkit
This program provides various string operations:
ORG 100h
main_menu:
; Clear screen
CALL clear_screen
; Display menu
MOV AH, 9
MOV DX, OFFSET menu_text
INT 21h
; Invalid option