Assembly 8086 Notes
The code snippets are in armasm because x86asm wouldn’t render. So ignore the syntax errors.
Emu8086
Section titled “Emu8086”Registers
Section titled “Registers”Memory slots used to store data
General Purpose Registers
Section titled “General Purpose Registers”- ax - the accumulator register (divided into ah / al)
- bx - the base address register (divided into bh / bl)
- cx - the count register (divided into ch / cl)
- dx - the data register (divided into dh / dl)
- si - source index register
- di - destination index register
- bp - base pointer
- sp - stack pointer
Segment Registers
Section titled “Segment Registers”- cs - points at the segment containing the current program
- ds - generally points at the segment where variables are defined
- es - extra segment register, it’s up to the programmer to define its usage
- ss - points at the segment containing the stack
All general purpose registers are 16 bit
In the 8086 assembly language, there are several registers, each serving a specific purpose. Here’s a brief description of each register and its typical usage:
General-Purpose Registers: AX (Accumulator): Used for arithmetic and data manipulation. It is also involved in some multiplication and division operations. BX (Base): Often used as a pointer to data or an offset in memory addressing. CX (Count): Primarily used as a loop counter in iterative operations. DX (Data): Like AX, it is used for arithmetic operations and sometimes holds the high-order bits of the result.
Segment Registers: CS (Code Segment): Points to the current code segment. DS (Data Segment): Points to the data segment where program variables are stored. SS (Stack Segment): Points to the stack segment, which holds the program stack. ES (Extra Segment): Used to store additional data segment addresses.
Index and Pointer Registers: SI (Source Index): Typically used as a pointer to the source data in string operations. DI (Destination Index): Usually used as a pointer to the destination in string operations. BP (Base Pointer): Frequently used to access function parameters and variables stored on the stack. SP (Stack Pointer): Points to the top of the stack and is involved in managing the stack.
Flags Register: FLAGS: Contains various status flags indicating the outcome of arithmetic and logical operations, as well as control flags for branching and looping.These registers are fundamental to 8086 assembly programming, and their usage may vary depending on the specific requirements of a program.
Data Types
Section titled “Data Types”- db - Define Byte
- dw - Define Word
- dd - Define Doubleword
- dq - Define Quadword
Examples
Section titled “Examples”a db 9message db 'hello world'var dw 1122h ; hexadecimal valuestring db "lorem ipsum", '$' ; '$' for end of stringstring1 db 10, 13, "lorem ipsum", '$' ; 10,13 for new lineArrays
Section titled “Arrays”Declaration
Section titled “Declaration”a db 1h, 2h, 3h, 7h ; int a[] = {1, 2, 3, 7}; Store a value from an array to the al registermov al, a[2]
; or with the help of the index rsgistermov si, 2mov al, a[si]Array Declaration using DUP
Section titled “Array Declaration using DUP”x db 3 dup(7)
; same as
x db 7, 7, 7y db 3 dup(5, 6)
; same as
y db 5, 6, 5, 6, 5, 6Declaring an empty array
Section titled “Declaring an empty array”var db 10 dup(?) ; int var[10] = {0}MOV instruction
Section titled “MOV instruction”It copies the second operand, called source, to the first operand called destination
mov ax, 7Types of operands supported
Section titled “Types of operands supported”;reg: ax, bx, ah, al, ch, cl, cx, di...etc;immediate: 7, -11, 4fh...etc;memory: [bx] or [bx+si] + displacement
mov reg, memorymov memory, regmov reg, regmov memory, immediatemov reg, immediate
;note -> mov memory, memory is not supportedMemory
Section titled “Memory”Combination of bx, si, di, bp registers inside of [] can be used to access memory
mov ax, 11hmov bx, 14hadd ax, bx ; ax = 25hThe result is stored in the destination (first register)
mov ch, 23hsub ch, 11h ; now ch = 12hFlags or Processor Status Registers
Section titled “Flags or Processor Status Registers”
16 bits. Each bit is called a “flag” and can be 0 or 1
- Carry Flag: Set to 1 when there is unsigned overflow
- Zero Flag: Set to 1 when result is zero, else it is set to 0
- Sign Flag: Set to 1 when the result is negative, else it is set to 0
Example
Section titled “Example”mov ch, 12hsub ch, 24hFlags: Z = 0 (result not zero), C = 1 (carry), S = 1 (result negative)
Multiplication assumes one of the operands is al or ax.
mov al, 7hmov bl, 7h
mul blThe result is stored in the al or ax register
It’s used for signed numbers
mov al, 35hmov bl, 7h
imul blIMUL uses the overflow flag
The result is stored in the al and ah registers respectively and div assumes one of the operands is ax.
mov ax, 0041hmov bl, 02h
div blax(41) / bl(2) = al(20) + ah(1)
It’s used for unsigned numbers
Logical Intructions
Section titled “Logical Intructions”- AND
- OR
- XOR
- NOT
- TEST
Using AND to determine if a number is even or odd
Section titled “Using AND to determine if a number is even or odd”It changes the value of the al register
mov al, 8hand al, 01h0000 1000
0000 0001
===AND===
0000 0000
So the number is even
mov al, 5hand al, 01h0000 0101
0000 0001
===AND===
0000 0001
So the number is odd
Like AND
It does not change the value of the al register
mov al, 7htest al, 01h; affects the flagThe other operands work as expected
Program Control Flow
Section titled “Program Control Flow”jmp, je, jle, jne, jz, jnz, ja, jb, jc
Unconditional Jumps
Section titled “Unconditional Jumps”They transfer control to another part of the program
Sample code
jmp read ;jump to read labelread: mov ah, 01 jmp exit
exit: mov ah, 4ch int 21hConditional Jumps
Section titled “Conditional Jumps”- Jump only when some condition is satisfied
- Most jumps work by affecting CPU FLAGs while jumps like jcxz depend on the register
je and jz - Jump when ZERO flag is equal to 1. They are more appropriate when you check whether something is 0 or not
jne and jnz - Jump when ZERO flag is 0. They are mostly used after a cmp instruction
ja and jg - Jump if above jump is greater
ja - Jump if CF = 0 and ZF = 0
jg - Jump if SF = OF and ZF = 0
jb - Jump if CF = 1
jc - Jump if CF = 1
jcxz - Jump id CX register is zero
Instructions
Section titled “Instructions”inc- Adds 1 to any registerdec- Subtracts 1 from any registercmp- subtract source form destination and set the flags appropriately
inc axdec bxcmp al, 10hBit Manipulation
Section titled “Bit Manipulation”shl- Shifts bits of byte to the left.
shl al, 1 ; shift al by 1 (use cl register if it's more than 1)shr- Shifts bits of byte to the right
shr al, 1 ; shift al by 1 (use cl register if it's more than 1)rol- Rotates the bits from the front to the back

mov cx, 7hrol ax, cl ; requires immidiate 8 bit operand or the cl register as the shift countror- Rotates the bits from the back to the front
mov cx, 7hror ax, cl ; requires immidiate 8 bit operand or the cl register as the shift countStandard code snippets
Section titled “Standard code snippets”Assume segment registers
Section titled “Assume segment registers”assume cs: code, ds: data, ss:stackInitalize ds
Section titled “Initalize ds”mov ax, datamov ds, axReturn control to OS
Section titled “Return control to OS”mov ah, 4chint 21hPrint string
Section titled “Print string”lea dx, msgmov ah, 9int 21hPrint integer
Section titled “Print integer”mov dl, intadd dl, 48mov ah, 2int 21hGet integer / character input
Section titled “Get integer / character input”Gets the ascii value
mov ah, 8hint 21hGet integer / character input and print it
Section titled “Get integer / character input and print it”Gets the ascii value
mov ah, 1int 21hSoftware interrupts
Section titled “Software interrupts”Before int 21h
- “Terminate program” - AH = 4Ch (76 decimal)
- “Display string” - AH = 09h (9 decimal)
- “Read keyboard input” - AH = 01h (1 decimal)
- “Write character to standard output” - AH = 02h (2 decimal)
- “Get system date” - AH = 2Ah (42 decimal)
- “Get system time” - AH = 2Ch (44 decimal)
- “Create a new file” - AH = 3Ch (60 decimal)
- “Open an existing file” - AH = 3Dh (61 decimal)
- “Close a file” - AH = 3Eh (62 decimal)
- “Read data from a file” - AH = 3Fh (63 decimal)
- “Write data to a file” - AH = 40h (64 decimal)
- “Delete a file” - AH = 41h (65 decimal)
- “Rename a file” - AH = 56h (86 decimal)
- “Get current disk drive” - AH = 19h (25 decimal)
- “Set disk transfer area address” - AH = 1Dh (29 decimal)
- “Get system time count” - AH = 2Dh (45 decimal)
- “Set system time” - AH = 25h (37 decimal)
- “Get current default drive” - AH = 19h, DL = 0 (25 decimal, DL = 0)
Processes
Section titled “Processes”[name] proc ... ... ... ret[name] endpTemplate file
Section titled “Template file”title [title]
assume cs:code, ds:data, ss:stack
stack segment stack db 256 dup(0)stack ends
data segment ; datadata ends
code segmentmain proc near mov ax, data mov ds, ax
mov ah, 4ch int 21hmain endpcode ends
end mainPrinting numbers
Section titled “Printing numbers”2 digits
Section titled “2 digits”print_two_digit_num proc push bx push cx
; assuming number is in ax mov cl, 10 div cl ; ax / cl -> ah, al
mov bx, ax
mov dl, bl add dl, 48 mov ah, 2 int 21h
mov dl, bh add dl, 48 mov ah, 2 int 21h
pop bx pop cx retprint_two_digit_num endp3 digits
Section titled “3 digits”print_three_digit_num proc push bx push cx
mov cl, 100 div cl
mov bx, ax
mov dl, bl add dl, '0' mov ah, 2h int 21h
xchg bl, bh mov bh, 0 mov ax, bx
mov cl, 10 div cl
mov bx, ax
mov dl, bl add dl, '0' mov ah, 2h int 21h
mov dl, bh add dl, '0' mov ah, 2h int 21h
pop bx pop cx
retprint_three_digit_num endp