Lab Manual
Lab Manual
Bahawalpur
Lab Manual
section .text
global _start
_start:
int 0x80
; call kernel
; exit(0)
; return code 0
int 0x80
; call kernel
2. Assembly code to add two numbers
section .data
section .bss
section .text
global _start
_start:
int 0x80
; call kernel
; Print a newline
mov eax, 4
int 0x80
; Exit
; call kernel
int 0x80
; return code 0
; call kernel
num1 db 2
num2 db 3
result db 0
; first number
; second number
section .bss
section .text
global _start
_start:
mul bl
of ax)
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
; call kernel
; Print a newline
mov eax, 4
mov ebx, 1
; Exit
section .text
global _start
_start:
xor ah, ah
div bl
mov [result], al
; Convert to ASCII
; Print result
int 0x80
; Print newline
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
; Exit program
int 0x80
section .text
global _start
_start:
sub al, bl ; AL = AL - BL
; Store result
mov [result], al
; Convert to ASCII
; Print result
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
; Print newline
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
; Exit
mov eax, 1
int 0x80
section .text
global _start
_start:
xchg al, bl
mov [num1], al
mov [num2], bl
mov [num1], al
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
mov [num1], al
int 0x80
mov [num2], al
int 0x80
; Print newline
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
; Exit
mov eax, 1
int 0x80
7. Assembly code to concatenate two numbers
section .data
section .text
global _start
_start:
mov [output], al
mov eax, 4
mov ebx, 1
int 0x80
; Exit
mov eax, 1
int 0x80
section .bss
section .text
global _start
_start:
; Print newline
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
mov [ascii_num], al
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
; Print newline
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
; Exit program
mov eax, 1
int 0x80
9. Loop Code
section .data
section .text
global _start
_start:
loop_start:
jge loop_end
mov bl, cl
; Multiply al by bl
mul bl
mov [result], al
mov edx, 1
int 0x80
; Output newline
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
jmp loop_start
loop_end:
; Exit program
int 0x80
global _start
_start:
; Print message
int 0x80
int 0x80
; Exit program
int 0x80
section .data
section .bss
sum resb 1
11. Arrays
section .text
global _start
_start:
sum_loop:
display:
; Exit
int 0x80
section .data
x: db 2, 4, 3 ; Array of bytes
_start:
; Convert characters '4' and '5' to integers
mov ecx, '4'
sub ecx, '0'
; Exit program
mov eax, 1 ; sys_exit
xor ebx, ebx ; return 0
int 0x80
; Procedure to add two numbers (ecx and edx), returns ASCII
of result in AL
sum:
mov eax, ecx
add eax, edx
add al, '0' ; Convert result to ASCII
ret
section .data
msg db "The sum is: ", 0xA
len equ $ - msg
section .bss
res resb 1
13. Condition
section .text
gcc
jg check_third_num
check_third_num:
jg _exit
_exit:
mov ecx,largest
mov edx, 2
mov eax, 1
int 80h
section .data
num1 dd '47'
num2 dd '22'
num3 dd '31'
segment .bss
largest resb 2
14. Recursion
section .text
global _start
_start:
; Print message
; Print result
; Exit
int 0x80
; Input: BX
; Output: AX = factorial(BX)
proc_fact:
cmp bl, 1
jg do_calculation
mov ax, 1
ret
do_calculation:
push bx ; Save BX
dec bl
pop bx ; Restore BX
mul bl ; AX *= BL
ret
section .data
section .bss
fact resb 1
global _start
_start:
; If number is odd
int 0x80
jmp exit_program
is_even:
int 0x80
exit_program:
int 0x80
section .data