0% found this document useful (0 votes)
2 views

Lab Manual

This document is a lab manual for a course on Computer Organization and Assembly Language at the Islamia University of Bahawalpur, specifically for the 4th semester. It includes various assembly code examples demonstrating basic operations such as printing strings, arithmetic operations (addition, subtraction, multiplication, division), and other programming concepts like loops, arrays, and procedures. Each code snippet is designed to illustrate fundamental programming techniques in assembly language.

Uploaded by

saif.khambra70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Manual

This document is a lab manual for a course on Computer Organization and Assembly Language at the Islamia University of Bahawalpur, specifically for the 4th semester. It includes various assembly code examples demonstrating basic operations such as printing strings, arithmetic operations (addition, subtraction, multiplication, division), and other programming concepts like loops, arrays, and procedures. Each code snippet is designed to illustrate fundamental programming techniques in assembly language.

Uploaded by

saif.khambra70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

The Islamia University of the

Bahawalpur

Department of Information & Communication


Engineering
[BS Cyber Security & Digital Forensics]

Lab Manual

Course : Computer Organization and Assembly Language

Semester : 4th Semester (M-1)

Submitted to: Ms. Akkasha Latif

Submitted by: Muhammad Saif Khan

Roll Number : F23BINCE1M04053


1. Assembly code to print a string “Hello world”
section .data

hello db 'Hello, world!',0 ; null-terminated string

section .text

global _start

_start:

; write(1, hello, 13)

mov eax, 4 ; system call number for sys_write

mov ebx, 1 ; file descriptor 1 is stdout

mov ecx, hello ; pointer to the string

mov edx, 13 ; number of bytes to write

int 0x80

; call kernel

; exit(0)

mov eax, 1 ; system call number for sys_exit

xor ebx, ebx

; return code 0

int 0x80

; call kernel
2. Assembly code to add two numbers
section .data

num1 db 5 ; first number

num2 db 3 ; second number

result db 0 ; space for the result

newline db 10 ; newline character

section .bss

section .text

global _start

_start:

; Load numbers into registers

mov al, [num1] ; load num1 into al

mov bl, [num2] ; load num2 into bl

; Add the numbers


add al, bl

; add bl to al, result in al

; Store the result

mov [result], al ; store the result

; Convert result to ASCII

add byte [result], '0' ; convert to ASCII

; Print the result

mov eax, 4 ; system call number for sys_write

mov ebx, 1 ; file descriptor 1 is stdout

mov ecx, result ; pointer to the result

mov edx, 1 ; number of bytes to write

int 0x80

; call kernel

; Print a newline

mov eax, 4

; system call number for sys_write

mov ebx, 1 ; file descriptor 1 is stdout

mov ecx, newline ; pointer to the newline character

mov edx, 1 ; number of bytes to write

int 0x80

; Exit

; call kernel

mov eax, 1 ; system call number for sys_exit


xor ebx, ebx

int 0x80

; return code 0

; call kernel

3. Assembly code to multiply two numbers


section .data

num1 db 2

num2 db 3

result db 0

; first number

; second number

; space for the result

newline db 10 ; newline character

section .bss
section .text

global _start

_start:

; Load numbers into registers

mov al, [num1] ; load num1 into al

mov bl, [num2] ; load num2 into bl

; Multiply the numbers

mul bl

; multiply al by bl, result in ax (al contains the lower 8 bits

of ax)

; Store the result

mov [result], al ; store the result

; Convert result to ASCII

add byte [result], '0' ; convert to ASCII

; Print the result

mov eax, 4

mov ebx, 1

; system call number for sys_write

; file descriptor 1 is stdout

mov ecx, result ; pointer to the result

mov edx, 1

; number of bytes to write

int 0x80
; call kernel

; Print a newline

mov eax, 4

mov ebx, 1

; system call number for sys_write

; file descriptor 1 is stdout

mov ecx, newline ; pointer to the newline character

mov edx, 1 ; number of bytes to write

int 0x80 ; call kernel

; Exit

mov eax, 1 ; system call number for sys_exit

xor ebx, ebx ; return code 0

int 0x80 ; call kernel

4. Assembly code to divide two numbers


section .data

num1 db 10 ; Numerator (dividend)

num2 db 2 ; Denominator (divisor)

result db 0 ; Space for result

newline db 10 ; Newline character

section .text

global _start

_start:

; Load num1 (dividend) into AL

mov al, [num1]

; Clear AH to make AX (AH:AL) a 16-bit register with value only in AL

xor ah, ah

; Load divisor into BL

mov bl, [num2]

; Divide AX by BL → AL = quotient, AH = remainder

div bl

; Store result (quotient) in memory

mov [result], al

; Convert to ASCII

add byte [result], '0'

; Print result

mov eax, 4 ; sys_write

mov ebx, 1 ; stdout


mov ecx, result ; address of result

mov edx, 1 ; 1 byte

int 0x80

; Print newline

mov eax, 4

mov ebx, 1

mov ecx, newline

mov edx, 1

int 0x80

; Exit program

mov eax, 1 ; sys_exit

xor ebx, ebx ; exit code 0

int 0x80

5. Assembly code to subtract two numbers


section .data

num1 db 7 ; First number

num2 db 4 ; Second number

result db 0 ; Space for result

newline db 10 ; Newline character

section .text

global _start

_start:

; Load num1 into AL

mov al, [num1]

; Load num2 into BL

mov bl, [num2]

; Subtract num2 from num1

sub al, bl ; AL = AL - BL

; Store result

mov [result], al

; Convert to ASCII

add byte [result], '0'

; Print result

mov eax, 4

mov ebx, 1

mov ecx, result

mov edx, 1
int 0x80

; Print newline

mov eax, 4

mov ebx, 1

mov ecx, newline

mov edx, 1

int 0x80

; Exit

mov eax, 1

xor ebx, ebx

int 0x80

6. Assembly code to swap number


section .data

num1 db 8 ; First number


num2 db 3 ; Second number

newline db 10 ; Newline character

section .text

global _start

_start:

; Load both numbers

mov al, [num1]

mov bl, [num2]

; Swap values using XCHG instruction

xchg al, bl

; Store swapped values

mov [num1], al

mov [num2], bl

; Convert num1 to ASCII and print

mov al, [num1]

add al, '0'

mov [num1], al

mov eax, 4

mov ebx, 1

mov ecx, num1

mov edx, 1

int 0x80

; Print space character between values


mov al, ' '

mov [num1], al

mov ecx, num1

int 0x80

; Convert num2 to ASCII and print

mov al, [num2]

add al, '0'

mov [num2], al

mov ecx, num2

int 0x80

; Print newline

mov eax, 4

mov ebx, 1

mov ecx, newline

mov edx, 1

int 0x80

; Exit

mov eax, 1

xor ebx, ebx

int 0x80
7. Assembly code to concatenate two numbers
section .data

num1 db 3 ; First digit

num2 db 5 ; Second digit

output db ' ', ' ', 10 ; Output buffer: 2 chars + newline

section .text

global _start

_start:

; Convert num1 to ASCII and store in output[0]

mov al, [num1]

add al, '0'

mov [output], al

; Convert num2 to ASCII and store in output[1]

mov al, [num2]


add al, '0'

mov [output + 1], al

; Print the 2-digit string

mov eax, 4

mov ebx, 1

mov ecx, output

mov edx, 3 ; 2 digits + newline

int 0x80

; Exit

mov eax, 1

xor ebx, ebx

int 0x80

8. Assembly code INC and DEC


section .data
num db 5 ; Original number (binary value)

newline db 10 ; Newline character (\n)

section .bss

ascii_num resb 1 ; To store ASCII character of number

section .text

global _start

_start:

; Increment the number

inc byte [num] ; num = num + 1

; Convert to ASCII and store in buffer

mov al, [num] ; Load value

add al, '0' ; Convert to ASCII

mov [ascii_num], al ; Store in buffer

; Print incremented number

mov eax, 4 ; sys_write

mov ebx, 1 ; stdout

mov ecx, ascii_num ; address of ASCII char

mov edx, 1 ; length = 1


int 0x80

; Print newline

mov eax, 4

mov ebx, 1

mov ecx, newline

mov edx, 1

int 0x80

; Decrement the number back

dec byte [num] ; num = num - 1

; Convert to ASCII and store in buffer again

mov al, [num]

add al, '0'

mov [ascii_num], al

; Print decremented number

mov eax, 4

mov ebx, 1

mov ecx, ascii_num

mov edx, 1

int 0x80
; Print newline

mov eax, 4

mov ebx, 1

mov ecx, newline

mov edx, 1

int 0x80

; Exit program

mov eax, 1

xor ebx, ebx

int 0x80

9. Loop Code
section .data

num1 db 2 ; First number

newline db 10 ; Newline character


section .bss

result resb 1 ; Space for the result

section .text

global _start

_start:

mov cl, 1 ; Counter initialized to 1

loop_start:

cmp cl, 6 ; Loop until counter is 6

jge loop_end

; Load num1 and counter into registers

mov al, [num1]

mov bl, cl

; Multiply al by bl

mul bl

; Store and convert result to ASCII

add al, '0'

mov [result], al

; Output the result

mov eax, 4 ; sys_write

mov ebx, 1 ; stdout

mov ecx, result

mov edx, 1

int 0x80
; Output newline

mov eax, 4

mov ebx, 1

mov ecx, newline

mov edx, 1

int 0x80

inc cl ; Increment counter

jmp loop_start

loop_end:

; Exit program

mov eax, 1 ; sys_exit

xor ebx, ebx

int 0x80

10. Numbers Code


section .text

global _start

_start:

; Convert ASCII '3' to integer

mov eax, '3'

sub eax, '0'

; Convert ASCII '4' to integer

mov ebx, '4'

sub ebx, '0'

; Add the integers

add eax, ebx

; Convert result to ASCII

add al, '0'

mov [sum], al ; Store ASCII character in sum

; Print message

mov eax, 4 ; sys_write

mov ebx, 1 ; file descriptor (stdout)

mov ecx, msg ; message


mov edx, len ; message length

int 0x80

; Print sum result

mov eax, 4 ; sys_write

mov ebx, 1 ; stdout

mov ecx, sum ; sum buffer

mov edx, 1 ; one character

int 0x80

; Exit program

mov eax, 1 ; sys_exit

xor ebx, ebx ; return code 0

int 0x80

section .data

msg db "The sum is: ", 0xA

len equ $ - msg

section .bss

sum resb 1
11. Arrays
section .text

global _start

_start:

mov eax, 3 ; Number of elements to sum

mov ebx, 0 ; EBX will store the sum

mov esi, x ; ESI points to the start of array 'x'

sum_loop:

add bl, [esi] ; Add current byte to BL (lower byte of EBX)

inc esi ; Move to next byte

dec eax ; Decrement count

jnz sum_loop ; Loop if EAX != 0

; Convert numeric sum to ASCII


add bl, '0'

mov [sum], bl ; Store ASCII result in 'sum'

display:

mov edx, 1 ; Message length = 1 byte

mov ecx, sum ; Message to print

mov ebx, 1 ; File descriptor (stdout)

mov eax, 4 ; sys_write

int 0x80 ; Call kernel

; Exit

mov eax, 1 ; sys_exit

xor ebx, ebx ; Return 0

int 0x80

section .data

x: db 2, 4, 3 ; Array of bytes

sum: db 0 ; To store ASCII sum


12. Procedures
section .text
global _start

_start:
; Convert characters '4' and '5' to integers
mov ecx, '4'
sub ecx, '0'

mov edx, '5'


sub edx, '0'

call sum ; Call sum function (adds ecx and edx)


mov [res], al ; Store result (as ASCII) in res

; Print the message


mov eax, 4 ; sys_write
mov ebx, 1 ; file descriptor (stdout)
mov ecx, msg ; message to write
mov edx, len ; message length
int 0x80

; Print the result


mov eax, 4 ; sys_write
mov ebx, 1 ; file descriptor (stdout)
mov ecx, res ; result to write
mov edx, 1 ; length = 1 byte
int 0x80

; 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

global _start ;must be declared for using

gcc

_start: ;tell linker entry point

mov ecx, [num1]

cmp ecx, [num2]

jg check_third_num

mov ecx, [num2]

check_third_num:

cmp ecx, [num3]

jg _exit

mov ecx, [num3]

_exit:

mov [largest], ecx


mov ecx,msg

mov edx, len

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov ecx,largest

mov edx, 2

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov eax, 1

int 80h

section .data

msg db "The largest digit is: ", 0xA,0xD

len equ $- msg

num1 dd '47'

num2 dd '22'

num3 dd '31'

segment .bss

largest resb 2
14. Recursion
section .text

global _start

_start:

mov bx, 3 ; Calculate factorial of 3

call proc_fact ; Result in AX

add al, '0' ; Convert to ASCII

mov [fact], al ; Store result in fact (1 byte)

; Print message

mov edx, len ; Message length

mov ecx, msg ; Message to display

mov ebx, 1 ; File descriptor (stdout)


mov eax, 4 ; sys_write

int 0x80 ; Call kernel

; Print result

mov edx, 1 ; 1 byte to print

mov ecx, fact ; Result address

mov ebx, 1 ; stdout

mov eax, 4 ; sys_write

int 0x80 ; Call kernel

; Exit

mov eax, 1 ; sys_exit

xor ebx, ebx ; Return 0

int 0x80

; Recursive factorial procedure

; 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

call proc_fact ; Recursive call

pop bx ; Restore BX

mul bl ; AX *= BL

ret

section .data

msg db 'Factorial 3 is: ', 0xA

len equ $ - msg

section .bss

fact resb 1

15. Logical Instructions


section .text

global _start

_start:

mov ax, 8 ; Store number in AX (e.g., 8)

and ax, 1 ; Check least significant bit

jz is_even ; If zero, number is even

; If number is odd

mov eax, 4 ; sys_write

mov ebx, 1 ; stdout

mov ecx, odd_msg ; message to write

mov edx, len2 ; length of message

int 0x80

jmp exit_program

is_even:

mov eax, 4 ; sys_write

mov ebx, 1 ; stdout

mov ecx, even_msg ; message to write

mov edx, len1 ; length of message

int 0x80
exit_program:

mov eax, 1 ; sys_exit

xor ebx, ebx ; return code 0

int 0x80

section .data

even_msg db 'Even Number!', 0xA

len1 equ $ - even_msg

odd_msg db 'Odd Number!', 0xA

len2 equ $ - odd_msg

You might also like