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

Programs

Uploaded by

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

Programs

Uploaded by

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

Sum of numbers in an array

section .data
array db 1, 2, 3, 4, 5 ; Example array of numbers
array_size equ $-array ; Calculate the size of the array
sum dw 0 ; Variable to store the sum

section .text
global _start

_start:
mov ecx, array_size ; Move the array size to ECX register
mov esi, 0 ; Initialize ESI register to point to the first element of the array

sum_loop:
add ax, [array + esi] ; Add the value at [array + esi] to AX register
add esi, 1 ; Increment ESI to point to the next element
loop sum_loop ; Loop until ECX becomes zero

mov [sum], ax ; Move the sum from AX to the 'sum' variable

; Now you can use the sum variable as required


; For example, you can print it using the syscalls

; Terminate the program


mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel
GCD of X numbers; user inputs
section .data
prompt db "Enter the number of elements: ", 0
num_prompt db "Enter number: ", 0
gcd_prompt db "The GCD of the numbers is: ", 0
max_elements equ 100 ; Maximum number of elements
numbers dd max_elements ; Array to store the input numbers
count dd 0 ; Number of input numbers
gcd dd 0 ; Variable to store the GCD

section .text
global _start

_start:
; Prompt the user to enter the number of elements
mov edi, prompt
call print_string

; Read the number of elements from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, count ; Buffer to store the number of elements
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


xor eax, eax ; Clear EAX register
mov edi, ecx ; Move the address of the input buffer to EDI
call convert_to_number ; Call the conversion function
mov ebx, eax ; Move the number of elements to EBX

; Loop to get the numbers


mov edi, num_prompt ; Prompt the user to enter a number

input_loop:
call print_string

; Read the number from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, numbers ; Buffer to store the input number
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


xor eax, eax ; Clear EAX register
mov edi, ecx ; Move the address of the input buffer to EDI
call convert_to_number ; Call the conversion function
mov [numbers + edi*4], eax ; Store the input number in the array
inc dword [count]

dec ebx ; Decrement the number of elements


jnz input_loop ; Loop until all numbers are entered

; Find the GCD of the numbers


mov eax, [numbers] ; Move the first number to EAX
mov ebx, eax ; Move the first number to EBX (initial GCD)
gcd_loop:
; Calculate GCD using Euclidean algorithm
xor edx, edx ; Clear EDX register
div dword [numbers + 4] ; Divide EAX by the next number in the array
mov eax, edx ; Move the remainder to EAX

; Check if the remainder is zero (found the GCD)


test eax, eax
jz exit_program

; Repeat the loop with the new values


xchg eax, ebx ; Swap EAX and EBX registers
loop gcd_loop

exit_program:
; Print the GCD
mov edi, gcd_prompt
call print_string

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov ecx, ebx ; Move the GCD value to ECX
add ecx, '0' ; Convert the GCD value to ASCII
mov edx, 1 ; Number of bytes to write
int 0x80 ; Call the kernel

; Terminate the program


mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to convert a string to a number


; Input: EDI - Address of the string
; Output: EAX - Converted number
convert_to_number:
xor eax, eax ; Clear EAX register (accumulated value)
xor ebx, ebx ; Clear EBX register (sign flag)
jmp skip_whitespace ; Jump to skip leading whitespace

convert_loop:
imul eax, 10 ; Multiply the accumulated value by 10
add eax, ebx ; Add the current digit to the accumulated value

skip_whitespace:
movzx ebx, byte [edi] ; Load the current character
cmp bl, ' ' ; Check if it is a whitespace character
je skip_conversion ; If yes, skip to the next character

cmp bl, '-' ; Check if it is a minus sign


je set_negative ; If yes, set the sign flag and skip to the next character

sub bl, '0' ; Convert the character to its numerical value


cmp bl, 0 ; Check if it is a valid digit
jb conversion_error ; If not, handle the conversion error

cmp bl, 9 ; Check if it is a valid digit


ja conversion_error ; If not, handle the conversion error
jmp convert_loop ; Jump to the next character

set_negative:
neg eax ; Negate the accumulated value
jmp convert_loop ; Jump to the next character

skip_conversion:
inc edi ; Move to the next character
cmp byte [edi], 0 ; Check if it is the null terminator
jne convert_loop ; If not, continue the conversion

ret

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov edx, -1 ; Length of the string (-1 for null-terminated)
int 0x80 ; Call the kernel

popa ; Restore registers


ret
Factors of Number
section .data
prompt db "Enter a number: ", 0
factors_prompt db "The factors of the number are: ", 0
max_factors equ 100 ; Maximum number of factors
factors dd max_factors ; Array to store the factors
num dd 0 ; Variable to store the input number
count dd 0 ; Number of factors found

section .text
global _start

_start:
; Prompt the user to enter a number
mov edi, prompt
call print_string

; Read the input number from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, num ; Buffer to store the input number
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


xor eax, eax ; Clear EAX register
mov edi, ecx ; Move the address of the input buffer to EDI
call convert_to_number ; Call the conversion function
mov ebx, eax ; Move the input number to EBX
; Find the factors of the number
mov eax, 1 ; Start with factor 1
mov ecx, ebx ; Initialize the loop counter with the input number

factor_loop:
mov edx, 0 ; Clear EDX register for division
div ecx ; Divide EBX by EAX
test edx, edx ; Check if the remainder is zero
jnz skip_factor ; If not zero, skip to the next factor

; Found a factor, store it in the factors array


mov [factors + 4 * eax], eax
inc dword [count]

skip_factor:
inc eax ; Increment the factor
loop factor_loop ; Loop until ECX becomes zero

; Print the factors


mov edi, factors_prompt
call print_string

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov ecx, factors ; Move the address of the factors array to ECX
mov edx, dword [count] ; Move the number of factors to EDX
shl edx, 2 ; Multiply by 4 to get the correct number of bytes
int 0x80 ; Call the kernel

; Terminate the program


mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to convert a string to a number


; Input: EDI - Address of the string
; Output: EAX - Converted number
convert_to_number:
xor eax, eax ; Clear EAX register (accumulated value)
xor ebx, ebx ; Clear EBX register (sign flag)
jmp skip_whitespace ; Jump to skip leading whitespace

convert_loop:
imul eax, 10 ; Multiply the accumulated value by 10
add eax, ebx ; Add the current digit to the accumulated value

skip_whitespace:
movzx ebx, byte [edi] ; Load the current character
cmp bl, ' ' ; Check if it is a whitespace character
je skip_conversion ; If yes, skip to the next character

cmp bl, '-' ; Check if it is a minus sign


je set_negative ; If yes, set the sign flag and skip to the next character

sub bl, '0' ; Convert the character to its numerical value


cmp bl, 0 ; Check if it is a valid digit
jb conversion_error ; If not, handle the conversion error

cmp bl, 9 ; Check if it is a valid digit


ja conversion_error ; If not, handle the conversion error
jmp convert_loop ; Jump to the next character

set_negative:
neg eax ; Negate the accumulated value
jmp convert_loop ; Jump to the next character

skip_conversion:
inc edi ; Move to the next character
cmp byte [edi], 0 ; Check if it is the null terminator
jne convert_loop ; If not, continue the conversion

ret

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov edx, -1 ; Length of the string (-1 for null-terminated)
int 0x80 ; Call the kernel

popa ; Restore registers


ret
Check if Perfect Number
section .data
prompt db "Enter a number: ", 0
perfect_prompt db "The number is a perfect number.", 0
not_perfect_prompt db "The number is not a perfect number.", 0
num dd 0 ; Variable to store the input number

section .text
global _start

_start:
; Prompt the user to enter a number
mov edi, prompt
call print_string

; Read the input number from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, num ; Buffer to store the input number
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


xor eax, eax ; Clear EAX register
mov edi, ecx ; Move the address of the input buffer to EDI
call convert_to_number ; Call the conversion function
mov ebx, eax ; Move the input number to EBX

; Check if the number is perfect


mov eax, 0 ; Initialize sum to zero
mov ecx, 1 ; Start with divisor 1

sum_loop:
cmp ecx, ebx ; Check if the divisor exceeds the input number
jae check_perfect ; If yes, jump to check if the number is perfect

mov edx, 0 ; Clear EDX register for division


div ecx ; Divide EBX by ECX
test edx, edx ; Check if the remainder is zero
jnz skip_sum ; If not zero, skip adding the divisor to the sum

add eax, ecx ; Add the divisor to the sum

skip_sum:
inc ecx ; Increment the divisor
jmp sum_loop ; Repeat the loop

check_perfect:
cmp eax, ebx ; Compare the sum with the input number
je is_perfect ; If equal, the number is perfect

; Print that the number is not a perfect number


mov edi, not_perfect_prompt
call print_string
jmp exit_program

is_perfect:
; Print that the number is a perfect number
mov edi, perfect_prompt
call print_string
exit_program:
; Terminate the program
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to convert a string to a number


; Input: EDI - Address of the string
; Output: EAX - Converted number
convert_to_number:
xor eax, eax ; Clear EAX register (accumulated value)
xor ebx, ebx ; Clear EBX register (sign flag)
jmp skip_whitespace ; Jump to skip leading whitespace

convert_loop:
imul eax, 10 ; Multiply the accumulated value by 10
add eax, ebx ; Add the current digit to the accumulated value

skip_whitespace:
movzx ebx, byte [edi] ; Load the current character
cmp bl, ' ' ; Check if it is a whitespace character
je skip_conversion ; If yes, skip to the next character

cmp bl, '-' ; Check if it is a minus sign


je set_negative ; If yes, set the sign flag and skip to the next character

sub bl, '0' ; Convert the character to its numerical value


cmp bl, 0 ; Check if it is a valid digit
jb conversion_error ; If not, handle the conversion error
cmp bl, 9 ; Check if it is a valid digit
ja conversion_error ; If not, handle the conversion error

jmp convert_loop ; Jump to the next character

set_negative:
neg eax ; Negate the accumulated value
jmp convert_loop ; Jump to the next character

skip_conversion:
inc edi ; Move to the next character
cmp byte [edi], 0 ; Check if it is the null terminator
jne convert_loop ; If not, continue the conversion

ret

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov edx, -1 ; Length of the string (-1 for null-terminated)
int 0x80 ; Call the kernel

popa ; Restore registers


ret
Check if Defective Number. Print deficiency if true
section .data
prompt db "Enter a number: ", 0
defective_prompt db "The number is a defective number with deficiency: ", 0
not_defective_prompt db "The number is not a defective number.", 0
num dd 0 ; Variable to store the input number

section .text
global _start

_start:
; Prompt the user to enter a number
mov edi, prompt
call print_string

; Read the input number from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, num ; Buffer to store the input number
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


xor eax, eax ; Clear EAX register
mov edi, ecx ; Move the address of the input buffer to EDI
call convert_to_number ; Call the conversion function
mov ebx, eax ; Move the input number to EBX

; Check if the number is defective


mov eax, 0 ; Initialize sum to zero
mov ecx, 1 ; Start with divisor 1

sum_loop:
cmp ecx, ebx ; Check if the divisor exceeds the input number
jae check_deficiency ; If yes, jump to check the deficiency

mov edx, 0 ; Clear EDX register for division


div ecx ; Divide EBX by ECX
add eax, edx ; Add the quotient to the sum

inc ecx ; Increment the divisor


jmp sum_loop ; Repeat the loop

check_deficiency:
sub eax, ebx ; Calculate the deficiency
jz is_not_defective ; If the deficiency is zero, the number is not defective

; Print that the number is a defective number with its deficiency


mov edi, defective_prompt
call print_string

mov eax, ebx ; Move the input number to EAX


call print_number ; Print the input number

mov eax, eax ; Move the deficiency to EAX


call print_number ; Print the deficiency

jmp exit_program
is_not_defective:
; Print that the number is not a defective number
mov edi, not_defective_prompt
call print_string

exit_program:
; Terminate the program
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to convert a string to a number


; Input: EDI - Address of the string
; Output: EAX - Converted number
convert_to_number:
xor eax, eax ; Clear EAX register (accumulated value)
xor ebx, ebx ; Clear EBX register (sign flag)
jmp skip_whitespace ; Jump to skip leading whitespace

convert_loop:
imul eax, 10 ; Multiply the accumulated value by 10
add eax, ebx ; Add the current digit to the accumulated value

skip_whitespace:
movzx ebx, byte [edi] ; Load the current character
cmp bl, ' ' ; Check if it is a whitespace character
je skip_conversion ; If yes, skip to the next character

cmp bl, '-' ; Check if it is a minus sign


je set_negative ; If yes, set the sign flag and skip to the next character
sub bl, '0' ; Convert the character to its numerical value
cmp bl, 0 ; Check if it is a valid digit
jb conversion_error ; If not, handle the conversion error

cmp bl, 9 ; Check if it is a valid digit


ja conversion_error ; If not, handle the conversion error

jmp convert_loop ; Jump to the next character

set_negative:
neg eax ; Negate the accumulated value
jmp convert_loop ; Jump to the next character

skip_conversion:
inc edi ; Move to the next character
cmp byte [edi], 0 ; Check if it is the null terminator
jne convert_loop ; If not, continue the conversion

ret

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov edx, -1 ; Length of the string (-1 for null-terminated)
int 0x80 ; Call the kernel
popa ; Restore registers
ret

; Helper function to print a number


; Input: EAX - Number to print
print_number:
pusha ; Save registers

mov ebx, 10 ; Divisor for decimal conversion

cmp eax, 0 ; Check if the number is zero


jne print_loop ; If not zero, continue printing

mov edi, '0' ; Move '0' to EDI


mov eax, 4 ; sys_write syscall number
mov ebx, 1 ; File descriptor 1 (stdout)
mov ecx, edi ; Character to print
mov edx, 1 ; Number of bytes to write
int 0x80 ; Call the kernel

jmp exit_print_number

print_loop:
xor edx, edx ; Clear EDX register for division
div ebx ; Divide EAX by EBX
add dl, '0' ; Convert the quotient to ASCII character

push edx ; Push the remainder to the stack


test eax, eax ; Check if the quotient is zero
jnz print_loop ; If not zero, continue printing

print_stack_loop:
pop edx ; Pop a digit from the stack
add dl, '0' ; Convert the digit to ASCII character

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov ecx, edx ; Character to print
mov edx, 1 ; Number of bytes to write
int 0x80 ; Call the kernel

cmp esp, ebp ; Check if the stack is empty


jne print_stack_loop ; If not empty, continue printing from the stack

exit_print_number:
popa ; Restore registers
ret
Check congruent number
section .data
prompt db "Enter a number: ", 0
congruent_prompt db "The number is a congruent number.", 0
not_congruent_prompt db "The number is not a congruent number.", 0
num dd 0 ; Variable to store the input number

section .text
global _start

_start:
; Prompt the user to enter a number
mov edi, prompt
call print_string

; Read the input number from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, num ; Buffer to store the input number
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


xor eax, eax ; Clear EAX register
mov edi, ecx ; Move the address of the input buffer to EDI
call convert_to_number ; Call the conversion function
mov ebx, eax ; Move the input number to EBX

; Check if the number is congruent


mov eax, ebx ; Move the input number to EAX
mov edx, 1 ; Start with divisor 1
xor ecx, ecx ; Clear ECX register (sum of divisors)

sum_loop:
mov eax, ebx ; Move the input number to EAX
mov ebx, edx ; Move the divisor to EBX
xor edx, edx ; Clear EDX register (remainder)

div ebx ; Divide EAX by EBX


cmp edx, 0 ; Check if the remainder is zero
jne skip_sum ; If not zero, skip adding the divisor to the sum

add ecx, ebx ; Add the divisor to the sum

skip_sum:
inc edx ; Increment the divisor
cmp edx, eax ; Check if the divisor exceeds the input number
jbe sum_loop ; If not, repeat the loop

cmp ecx, ebx ; Compare the sum with the input number
je is_congruent ; If equal, the number is congruent

; Print that the number is not a congruent number


mov edi, not_congruent_prompt
call print_string
jmp exit_program

is_congruent:
; Print that the number is a congruent number
mov edi, congruent_prompt
call print_string

exit_program:
; Terminate the program
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to convert a string to a number


; Input: EDI - Address of the string
; Output: EAX - Converted number
convert_to_number:
xor eax, eax ; Clear EAX register (accumulated value)
xor ebx, ebx ; Clear EBX register (sign flag)
jmp skip_whitespace ; Jump to skip leading whitespace

convert_loop:
imul eax, 10 ; Multiply the accumulated value by 10
add eax, ebx ; Add the current digit to the accumulated value

skip_whitespace:
movzx ebx, byte [edi] ; Load the current character
cmp bl, ' ' ; Check if it is a whitespace character
je skip_conversion ; If yes, skip to the next character

cmp bl, '-' ; Check if it is a minus sign


je set_negative ; If yes, set the sign flag and skip to the next character

sub bl, '0' ; Convert the character to its numerical value


cmp bl, 0 ; Check if it is a valid digit
jb conversion_error ; If not, handle the conversion error

cmp bl, 9 ; Check if it is a valid digit


ja conversion_error ; If not, handle the conversion error

jmp convert_loop ; Jump to the next character

set_negative:
neg eax ; Negate the accumulated value
jmp convert_loop ; Jump to the next character

skip_conversion:
inc edi ; Move to the next character
cmp byte [edi], 0 ; Check if it is the null terminator
jne convert_loop ; If not, continue the conversion

ret

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov edx, -1 ; Length of the string (-1 for null-terminated)
int 0x80 ; Call the kernel
popa ; Restore registers
ret
Implement calculator using procedures; user chooses the operation;
section .data
prompt db "Choose an operation:", 0
add_prompt db "1. Addition", 0
sub_prompt db "2. Subtraction", 0
mul_prompt db "3. Multiplication", 0
div_prompt db "4. Division", 0
result_prompt db "Result: ", 0
error_prompt db "Invalid input!", 0

section .text
global _start

_start:
; Display the menu to the user
mov edi, prompt
call print_string
mov edi, add_prompt
call print_string
mov edi, sub_prompt
call print_string
mov edi, mul_prompt
call print_string
mov edi, div_prompt
call print_string

; Read the user's choice


call read_integer
mov ebx, eax
; Perform the corresponding operation
cmp ebx, 1
je perform_addition
cmp ebx, 2
je perform_subtraction
cmp ebx, 3
je perform_multiplication
cmp ebx, 4
je perform_division
jmp print_error

perform_addition:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax

; Perform the addition


add ecx, edx

; Print the result


mov edi, result_prompt
call print_string
mov eax, ecx
call print_number

jmp exit_program
perform_subtraction:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax

; Perform the subtraction


sub ecx, edx

; Print the result


mov edi, result_prompt
call print_string
mov eax, ecx
call print_number

jmp exit_program

perform_multiplication:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax

; Perform the multiplication


imul ecx, edx

; Print the result


mov edi, result_prompt
call print_string
mov eax, ecx
call print_number

jmp exit_program

perform_division:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax

; Check for division by zero


cmp edx, 0
je division_by_zero

; Perform the division


idiv ecx, edx

; Print the result


mov edi, result_prompt
call print_string
mov eax, ecx
call print_number

jmp exit_program

division_by_zero:
; Print an error message for division by zero
mov edi, error_prompt
call print_string
jmp exit_program

print_error:
; Print an error message for invalid input
mov edi, error_prompt
call print_string

exit_program:
; Terminate the program
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to read an integer from the user


; Output: EAX - The read integer value
read_integer:
; Clear EAX register
xor eax, eax

; Read a character from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, input_buffer ; Buffer to store the input
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


call convert_to_number
ret

; Helper function to convert a string to a number


; Input: EDI - Address of the string
; Output: EAX - Converted number
convert_to_number:
xor eax, eax ; Clear EAX register (accumulated value)
xor ebx, ebx ; Clear EBX register (sign flag)
jmp skip_whitespace ; Jump to skip leading whitespace

convert_loop:
imul eax, 10 ; Multiply the accumulated value by 10
add eax, ebx ; Add the current digit to the accumulated value

skip_whitespace:
movzx ebx, byte [edi] ; Load the current character
cmp bl, ' ' ; Check if it is a whitespace character
je skip_conversion ; If yes, skip to the next character

cmp bl, '-' ; Check if it is a minus sign


je set_negative ; If yes, set the sign flag and skip to the next character

sub bl, '0' ; Convert the character to its numerical value


cmp bl, 0 ; Check if it is a valid digit
jb conversion_error ; If not, handle the conversion error

cmp bl, 9 ; Check if it is a valid digit


ja conversion_error ; If not, handle the conversion error
jmp convert_loop ; Jump to the next character

set_negative:
neg eax ; Negate the accumulated value
jmp convert_loop ; Jump to the next character

skip_conversion:
inc edi ; Move to the next character
cmp byte [edi], 0 ; Check if it is the null terminator
jne convert_loop ; If not, continue the conversion

ret

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
xor ecx, ecx ; Clear ECX register (counter)
not ecx ; Set ECX to -1 for null-terminated string
int 0x80 ; Call the kernel

popa ; Restore registers


ret

; Helper function to print a number


; Input: EAX - Number to print
print_number:
pusha ; Save registers

mov ecx, 10 ; Set divisor to 10


xor edx, edx ; Clear EDX register (remainder)
mov edi, 0 ; Counter for number of digits

cmp eax, 0 ; Check if the number is zero


jg check_digits ; If not zero, continue

mov dl, '0' ; Otherwise, print '0'


mov eax, 1 ; Set counter to 1
call print_digit ; Call the digit printing function
jmp exit_print_number

check_digits:
; Count the number of digits
mov ebx, eax ; Move the number to EBX
count_digits:
xor edx, edx ; Clear EDX register (remainder)
div ecx ; Divide by 10
inc edi ; Increment the counter
test ebx, ebx ; Check if the number is zero
jnz count_digits ; If not zero, continue counting

; Print each digit


print_digits:
xor edx, edx ; Clear EDX register (remainder)
div ecx ; Divide by 10
add dl, '0' ; Convert the remainder to ASCII
call print_digit ; Call the digit printing function
dec edi ; Decrement the counter
test eax, eax ; Check if the number is zero
jnz print_digits ; If not zero, continue printing

exit_print_number:
popa ; Restore registers
ret

; Helper function to print a single digit


; Input: DL - Digit to print
print_digit:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov ecx, digit_buffer ; Buffer to store the digit
mov byte [ecx], dl ; Store the digit in the buffer
mov edx, 1 ; Number of bytes to write
int 0x80 ; Call the kernel

popa ; Restore registers


ret

section .bss
input_buffer resb 4 ; Buffer to store the user's input
digit_buffer resb 1 ; Buffer to store a single digit
Concatenate your name (taken from user) to roll number (stored in
memory)
section .data
prompt db "Enter your name: ", 0
roll_number db "123456", 0 ; Example roll number stored in memory

section .bss
name resb 32 ; Buffer to store the user's name

section .text
global _start

_start:
; Prompt the user to enter their name
mov edi, prompt
call print_string

; Read the user's name from the input


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, name ; Buffer to store the user's name
mov edx, 32 ; Maximum number of bytes to read
int 0x80 ; Call the kernel

; Find the end of the roll number


mov edi, roll_number ; Start address of the roll number
find_end:
cmp byte [edi], 0 ; Check if the byte is the null terminator
je concat_name ; If yes, move to concatenation

inc edi ; Move to the next byte


jmp find_end ; Continue searching

concat_name:
; Move the user's name after the roll number
mov esi, name ; Start address of the user's name
copy_loop:
mov al, byte [esi] ; Copy a byte from the user's name
mov byte [edi], al ; Store the byte after the roll number

cmp al, 0 ; Check if the byte is the null terminator


je print_concatenated ; If yes, move to printing

inc esi ; Move to the next byte in the user's name


inc edi ; Move to the next available byte
jmp copy_loop ; Continue copying

print_concatenated:
; Print the concatenated name and roll number
mov edi, roll_number
call print_string

mov edi, name


call print_string

exit_program:
; Terminate the program
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
xor ecx, ecx ; Clear ECX register (counter)
not ecx ; Set ECX to -1 for null-terminated string
int 0x80 ; Call the kernel

popa ; Restore registers


ret
Volume, Surface Area etc of object.
section .data
prompt db "Enter the side length of the cube: ", 0
volume_prompt db "Volume of the cube: ", 0
surface_area_prompt db "Surface area of the cube: ", 0
side_length dd 0 ; Variable to store the side length of the cube

section .text
global _start

_start:
; Prompt the user to enter the side length of the cube
mov edi, prompt
call print_string

; Read the side length from the user


mov eax, 3 ; sys_read syscall number
mov ebx, 0 ; File descriptor 0 (stdin)
mov ecx, side_length ; Buffer to store the side length
mov edx, 4 ; Number of bytes to read
int 0x80 ; Call the kernel

; Convert the input to a number


xor eax, eax ; Clear EAX register
mov edi, ecx ; Move the address of the input buffer to EDI
call convert_to_number ; Call the conversion function
mov ebx, eax ; Move the input number to EBX

; Calculate the volume of the cube


mov eax, ebx ; Move the side length to EAX
mul ebx ; Multiply the side length by itself
mul ebx ; Multiply the result by the side length again

; Print the volume of the cube


mov edi, volume_prompt
call print_string

mov eax, edx ; Move the result to EAX


call print_number

; Calculate the surface area of the cube


mov eax, ebx ; Move the side length to EAX
mul ebx ; Multiply the side length by itself
mov ebx, eax ; Move the result to EBX
shl eax, 1 ; Multiply the result by 2
add eax, ebx ; Add the result to the original result

; Print the surface area of the cube


mov edi, surface_area_prompt
call print_string

mov eax, edx ; Move the result to EAX


call print_number

exit_program:
; Terminate the program
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit status 0
int 0x80 ; Call the kernel

; Helper function to convert a string to a number


; Input: EDI - Address of the string
; Output: EAX - Converted number
convert_to_number:
xor eax, eax ; Clear EAX register (accumulated value)
xor ebx, ebx ; Clear EBX register (sign flag)
jmp skip_whitespace ; Jump to skip leading whitespace

convert_loop:
imul eax, 10 ; Multiply the accumulated value by 10
add eax, ebx ; Add the current digit to the accumulated value

skip_whitespace:
movzx ebx, byte [edi] ; Load the current character
cmp bl, ' ' ; Check if it is a whitespace character
je skip_conversion ; If yes, skip to the next character

cmp bl, '-' ; Check if it is a minus sign


je set_negative ; If yes, set the sign flag and skip to the next character

sub bl, '0' ; Convert the character to its numerical value


cmp bl, 0 ; Check if it is a valid digit
jb conversion_error ; If not, handle the conversion error

cmp bl, 9 ; Check if it is a valid digit


ja conversion_error ; If not, handle the conversion error

jmp convert_loop ; Jump to the next character

set_negative:
neg eax ; Negate the accumulated value
jmp convert_loop ; Jump to the next character

skip_conversion:
inc edi ; Move to the next character
cmp byte [edi], 0 ; Check if it is the null terminator
jne convert_loop ; If not, continue the conversion

ret

; Helper function to print a null-terminated string


; Input: EDI - Address of the string to print
print_string:
pusha ; Save registers
mov eax, 4 ; sys_write syscall number
mov ebx, 1 ; File descriptor 1 (stdout)
xor ecx, ecx ; Clear ECX register (counter)
not ecx ; Set ECX to -1 for null-terminated string
int 0x80 ; Call the kernel

popa ; Restore registers


ret

; Helper function to print a number


; Input: EAX - Number to print
print_number:
pusha ; Save registers

mov ecx, 10 ; Set divisor to 10


xor edx, edx ; Clear EDX register (remainder)
mov edi, 0 ; Counter for number of digits

cmp eax, 0 ; Check if the number is zero


jg check_digits ; If not zero, continue

mov dl, '0' ; Otherwise, print '0'


mov eax, 1 ; Set counter to 1
call print_digit ; Call the digit printing function
jmp exit_print_number
check_digits:
; Count the number of digits
mov ebx, eax ; Move the number to EBX
count_digits:
xor edx, edx ; Clear EDX register (remainder)
div ecx ; Divide by 10
inc edi ; Increment the counter
test ebx, ebx ; Check if the number is zero
jnz count_digits ; If not zero, continue counting

; Print each digit


print_digits:
xor edx, edx ; Clear EDX register (remainder)
div ecx ; Divide by 10
add dl, '0' ; Convert the remainder to ASCII
call print_digit ; Call the digit printing function

dec edi ; Decrement the counter


test eax, eax ; Check if the number is zero
jnz print_digits ; If not zero, continue printing

exit_print_number:
popa ; Restore registers
ret
; Helper function to print a single digit
; Input: DL - Digit to print
print_digit:
pusha ; Save registers

mov eax, 4 ; sys_write syscall number


mov ebx, 1 ; File descriptor 1 (stdout)
mov ecx, digit_buffer ; Buffer to store the digit
mov byte [ecx], dl ; Store the digit in the buffer
mov edx, 1 ; Number of bytes to write
int 0x80 ; Call the kernel

popa ; Restore registers


ret

section .bss
digit_buffer resb 1 ; Buffer to store a single digit

You might also like