Programs
Programs
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
section .text
global _start
_start:
; Prompt the user to enter the number of elements
mov edi, prompt
call print_string
input_loop:
call print_string
exit_program:
; Print the GCD
mov edi, gcd_prompt
call print_string
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
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
section .text
global _start
_start:
; Prompt the user to enter a number
mov edi, prompt
call print_string
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
skip_factor:
inc eax ; Increment the factor
loop factor_loop ; Loop until ECX becomes zero
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
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
section .text
global _start
_start:
; Prompt the user to enter a number
mov edi, prompt
call print_string
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
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
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
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
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
section .text
global _start
_start:
; Prompt the user to enter a number
mov edi, prompt
call print_string
sum_loop:
cmp ecx, ebx ; Check if the divisor exceeds the input number
jae check_deficiency ; If yes, jump to check the deficiency
check_deficiency:
sub eax, ebx ; Calculate the deficiency
jz is_not_defective ; If the deficiency is zero, the number is not defective
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
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
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
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
print_stack_loop:
pop edx ; Pop a digit from the stack
add dl, '0' ; Convert the digit to ASCII character
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
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)
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
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
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
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
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
perform_addition:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax
jmp exit_program
perform_subtraction:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax
jmp exit_program
perform_multiplication:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax
jmp exit_program
perform_division:
; Read the two numbers
call read_integer
mov ecx, eax
call read_integer
mov edx, eax
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
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
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
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
exit_print_number:
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
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
print_concatenated:
; Print the concatenated name and roll number
mov edi, roll_number
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
section .text
global _start
_start:
; Prompt the user to enter the side length of the cube
mov edi, 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
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
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
exit_print_number:
popa ; Restore registers
ret
; Helper function to print a single digit
; Input: DL - Digit to print
print_digit:
pusha ; Save registers
section .bss
digit_buffer resb 1 ; Buffer to store a single digit