How to create a hello world program
.model small
.stack 100h
.data
msg db 'Hello, World!', 0
.code
main proc
mov ax, @data
mov ds, ax
lea dx, msg
mov ah, 09h
int 21h
mov ah, 4Ch
int 21h
main endp
end main
Let me explain what each line does:
1. .model small: This line specifies the memory model for the program. In this case, we're using
the small model.
2. .stack 100h: This line sets the size of the stack segment.
3. .data: This line begins the data segment, where we define our variables.
4. msg db 'Hello, World!', 0: This line defines a byte array msg containing the string "Hello,
World!" followed by a null terminator (0).
5. .code: This line begins the code segment, where we write our program's instructions.
6. main proc: This line defines the main procedure, which is the entry point for our program.
7. mov ax, @data: This line moves the address of the data segment into the ax register.
8. mov ds, ax: This line moves the value of ax into the ds register, which sets the data segment
register.
9. lea dx, msg: This line loads the effective address of the msg array into the dx register.
10. mov ah, 09h: This line moves the value 09h into the ah register, which is the function code
for printing a string.
11. int 21h: This line invokes the DOS interrupt 21h, which executes the function specified in ah.
12. mov ah, 4Ch: This line moves the value 4Ch into the ah register, which is the function code
for exiting the program.
13. int 21h: This line invokes the DOS interrupt 21h again, which exits the program.
14. main endp: This line ends the main procedure.
15. end main: This line ends the program.
VVM Programs using “BRP”, “BRZ” & “BR” instructions
.model small
.stack 100h
.data num db 5 ; Number to check
.code
main:
mov ax, @data
mov ds, ax
mov al, num ; Load the number into AL register
cmp al, 0 ; Compare AL with 0
brp Positive ; Branch if Positive (AL > 0)
brz Zero ; Branch if Zero (AL == 0)
Negative: ; Code for negative number
mov ah, 09h
lea dx, negMsg
int 21h jmp
Done
Positive:
; Code for positive number
mov ah, 09h
lea dx, posMsg
int 21h
jmp Done
Zero:
; Code for zero
mov ah, 09h
lea dx, zeroMsg
int 21h
Done:
mov ah, 4Ch
int 21h
.data
negMsg db 'The number is negative.$'
posMsg db 'The number is positive.$'
zeroMsg db 'The number is zero.$'
The VVM (Virtual Machine) Programs typically use conditional branching instructions like BRP,
BRZ, and BR to control the flow of execution in assembly language programs. These instructions
are commonly found in various assembly languages, including the 8086 instruction set.
Here’s a quick breakdown of these instructions:
1. BRP (Branch if Positive): This instruction will cause a branch if the result of the last
operation was positive (i.e., the sign flag is cleared).
2. BRZ (Branch if Zero): This instruction will cause a branch if the result of the last
operation was zero (i.e., the zero flag is set).
3. BR (Unconditional Branch): This instruction causes an unconditional jump to the target
address.
Arithmetic operations in Assembly Language Addition and Subtraction
.model small
.stack 100h
.data
num1 db 10 ; First number
num2 db 5 ; Second number
result db 0 ; To store result
.code
main:
mov ax, @data
mov ds, ax
; Load num1 and num2 into registers and add
mov al, [num1] ; Load num1 into AL
add al, [num2] ; AL = AL + num2
mov [result], al ; Store the result
; Now perform subtraction
mov al, [num1] ; Reload num1 into AL
sub al, [num2] ; AL = AL - num2
; Result is stored in AL
; Exit Program
mov ah, 4Ch
int 21h
end main
Implementation of If-Then-Else Instruction in Assembly Language
section .data
msg1 db "A is greater", 0 ; Message for Then block
msg2 db "B is greater or equal", 0 ; Message for Else block
section .bss
a resb 1
b resb 1
section .text
global _start
_start:
; Assign values to a and b
mov byte [a], 5 ; a = 5
mov byte [b], 3 ; b = 3
; Load a and b into registers
mov al, [a]
mov bl, [b]
; Compare a and b
cmp al, bl
jf greater_than ; If a > b, jump to greater_than block
; Else block (a <= b)
mov edx, 20 ; Message length
mov ecx, msg2 ; Load message2
jmp print_message ; Jump to print
greater_than:
mov edx, 14 ; Message length
mov ecx, msg1 ; Load message1
print_message:
mov ebx, 1 ; File descriptor (stdout)
mov eax, 4 ; sys_write syscall
int 0x80 ; Call kernel
; Exit program
mov eax, 1 ; sys_exit syscall
xor ebx, ebx ; Exit code 0
String display and enter instruction
.MODEL SMALL
.STACK 100H
.DATA
buffer db 20 ; Max characters to input
db ? ; Store actual length of input
db 20 DUP('$') ; Buffer for characters
msg db "You entered: $"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; Input string from user
MOV DX, OFFSET buffer
MOV AH, 0AH
INT 21H
; Display message
MOV DX, OFFSET msg
MOV AH, 09H
INT 21H
; Print user input
MOV DX, OFFSET buffer+2 ; Start of user input
MOV AH, 09H
INT 21H
; Exit program
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
Implementation of Loop Instruction Print "Hello" Five Times
.MODEL SMALL
.STACK 100H
.DATA
msg db "Hello$" ; String to print
.CODE
MAIN PROC
MOV AX, @DATA ; Load data segment
MOV DS, AX
MOV CX, 5 ; Set loop counter to 5
print_hello:
MOV DX, OFFSET msg ; Load message address
MOV AH, 09H ; DOS service for string display
INT 21H ; Call interrupt
MOV AH, 02H ; DOS service for new line
MOV DL, 0AH ; ASCII for new line (LF)
INT 21H
MOV DL, 0DH ; ASCII for carriage return (CR)
INT 21H
LOOP print_hello ; Decrease CX and repeat if not zero
MOV AH, 4CH ; DOS terminate program
INT 21H
MAIN ENDP
END MAIN
Sum of First 10 Numbers (1+2+3+...+10)
.MODEL SMALL
.STACK 100H
.DATA
sum db 0 ; Variable to store sum
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV CX, 10 ; Loop counter (1 to 10)
MOV AL, 0 ; AL stores sum
MOV BL, 1 ; Start with number 1
add_numbers:
ADD AL, BL ; Add BL to AL
INC BL ; Increment BL (next number)
LOOP add_numbers ; Repeat until CX = 0
MOV sum, AL ; Store final sum
MOV AH, 4CH ; Exit program
INT 21H
MAIN ENDP
END MAIN
Concept of the STACK in Assembly Language
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AX, 1234H ; Load AX with 1234H
MOV BX, 5678H ; Load BX with 5678H
MOV CX, 9ABCH ; Load CX with 9ABCH
PUSH AX ; Store AX on stack
PUSH BX ; Store BX on stack
PUSH CX ; Store CX on stack
POP DI ; Retrieve CX into DI
POP SI ; Retrieve BX into SI
POP DX ; Retrieve AX into DX
MOV AH, 4CH ; Exit program
INT 21H
MAIN ENDP
END MAIN