Uploaded by Bisma Ghaffar

coal 12

advertisement
Bisma ghaffar
02-136221-030
LAB 12:
Q1) Write a program to generate positive number s from 1-20 using loop.
CODE:
section .data
output_format db "%d ",
section .text
global _start
_start:
mov eax, 1
generate_numbers_loop:
cmp eax, 21
jg end_program
; Print the current number
push eax
push output_format
call printf
add esp, 8
; Increment the number
inc eax
jmp generate_numbers_loop
end_program:
; Exit the program
mov eax, 1
xor ebx, ebx
int 0x80
section .text
extern printf
global main
main:
push ebp
mov ebp, esp
and esp, 0FFFFFFF0h
sub esp, 16
call _start
mov esp, ebp
pop ebp
ret
OUTPUT:
Q2) Write a program to take user input number and find i ts factorial.
CODE:
section .data
input_msg db "Enter a number: ", 0
output_msg db "Factorial: %d", 10, 0
section .bss
user_input resd 1
section .text
global _star
_start:
mov eax, 4
mov ebx, 1
mov ecx, input_msg
mov edx, 16
int 0x80
; Read user input
mov eax, 3
mov ebx, 0
mov ecx, user_input
mov edx, 4
int 0x80
mov eax, [user_input]
sub eax, '0'
mov [user_input], eax
; Calculate factorial
mov eax, 1
mov ecx, [user_input]
calculate_factorial:
cmp ecx, 1
jbe display_result
imul eax, ecx
dec ecx
jmp calculate_factorial
display_result:
; Display the result
mov eax, 4
mov ebx, 1
mov ecx, output_msg
mov edx, 16
int 0x80
; Exit the program
mov eax, 1
xor ebx, ebx
int 0x80
OUTPUT
Q3) Write a program to take user input number and display the sum from 1 to given number.
CODE:
section .data
input_msg db "Enter a number: ", 0
output_msg db "Sum: %d", 10, 0
section .bss
user_input resd 1 ; Reserve space for the user input
sum resd 1
; Reserve space for the sum
section .text
global _start
_start:
; Display prompt to enter a number
mov eax, 4
mov ebx, 1
mov ecx, input_msg
mov edx, 16
int 0x80
; Read user input
mov eax, 3
mov ebx, 0
mov ecx, user_input
mov edx, 4
int 0x80
; Convert the input to an integer
mov eax, [user_input]
sub eax, '0'
mov [user_input], eax
; Calculate the sum
mov eax, 0
; Initialize sum to 0
mov ecx, [user_input]
calculate_sum:
add eax, ecx
; Add current value of ecx to sum
loop calculate_sum ; Decrement ecx and repeat the loop until ecx is zero
; Store the result in the 'sum' variable
mov [sum], eax
; Display the result
mov eax, 4
mov ebx, 1
mov ecx, output_msg
mov edx, 16
int 0x80
; Exit the program
mov eax, 1
xor ebx, ebx
int 0x80
OUTPUT:
Download