Assembly Program to Input 5 numbers and print them.
01Assignment Assembly Programming
Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from the user and store them in an array and display the accepted numbers.
section .data
msg1 db "Program to accept 5 numbers and print them!",0xA ; message to be printed at the beginning of the program
len1 equ $-msg1 ; length of msg1
msg2 db "Enter 5 numbers:" ,0xA ; message to prompt the user to enter the numbers
len2 equ $-msg2 ; length of msg2
msg3 db "Displaying 5 numbers:" ,0xA ; message to indicate that the numbers are being displayed
len3 equ $-msg3 ; length of msg3
cnt db 05h ; counter to track the number of times the loop needs to be executed
mov r8,00h ; initializing r8 to 0
section .bss
num resb 16 ; array to store the user input
section .text
global _start
_start :
Msg1:
mov rax,01 ; write instruction
mov rdi,01 ; stdout file descriptor
mov rsi,msg1 ; message to print
mov rdx,len1 ; length of message
syscall ; system call to print the message
InputMsg:
mov rax,01
mov rdi,01
mov rsi,msg2
mov rdx,len2
syscall
mov r8,num ; r8 pointing to num array
InputNum:
mov rax,00 ; read instruction
mov rdi,00 ; stdin file descriptor
mov rsi,r8 ; memory location where the input is to be stored
mov rdx,17 ; number of bytes to be read (16 bytes for 64 bit hexadecimal number and 1 byte for newline character)
syscall ; system call to read the input
add r8,17 ; incrementing the pointer by 17 bytes to point to the next memory location to store the next input
dec byte[cnt] ; decrementing the counter by 1
jnz InputNum ; if counter is not zero, repeat the loop
OutputMsg:
mov rax,01
mov rdi,01
mov rsi,msg3
mov rdx,len3
syscall
mov byte[cnt],05h ; reinitialize cnt to 05
mov r8,num ; point r8 to start of num array
OutputNum:
mov rax,01
mov rdi,01
mov rsi,r8
mov rdx,17
syscall
add r8,17 ; incrementing the pointer by 17 bytes to point to the next memory
dec byte[cnt] ; decrementing the counter by 1
jnz OutputNum ; if counter is not zero, repeat the loop
Exit:
mov rax,60 ; exit system call
mov rdi,0 ; exit status
syscall ; system call to exit the program
Output:
You can understand the code above from the code and comments. Below is full detailed explaination.
section .data
msg1 db "Program to accept 5 numbers and print them!",0xA
len1 equ $-msg1
msg2 db "Enter 5 numbers:" ,0xA
len2 equ $-msg2
msg3 db "Displaying 5 numbers:" ,0xA
len3 equ $-msg3
cnt db 05h
mov r8,00h
Here we have the .data
section where we define our messages and constants. msg1
, msg2
, and msg3
are our messages and len1
, len2
, and len3
are the length of each message respectively. cnt
is a counter to keep track of the number of times the user inputs a value. r8
is used to store the address of the num
array.
section .bss
num resb 16 ;array
Here we have the .bss
section where we define the num
array which will hold the 5 hexadecimal numbers input by the user. It is defined as a 16-byte array since each hexadecimal number is 64-bit, and we have to store 5 of them.
section .text
global _start
_start :
Here we have the .text
section where we define our program's code. _start
is the entry point of our program.
Msg1:
mov rax,01
mov rdi,01
mov rsi,msg1
mov rdx,len1
syscall
This section is responsible for printing the first message that explains the program's purpose. We use the syscall
instruction with rax
equal to 1 to write to the standard output (screen).
InputMsg:
mov rax,01
mov rdi,01
mov rsi,msg2
mov rdx,len2
syscall
Here we print the second message which prompts the user to input 5 numbers.
mov r8,num
InputNum:
mov rax,00
mov rdi,00
mov rsi,r8
mov rdx,17
syscall
add r8,17
dec byte[cnt]
jnz InputNum
This is the loop that inputs the 5 numbers into the num
array. We use the syscall
instruction with rax
equal to 0 to read from the standard input (keyboard).
In each iteration, rsi
is set to the current position of the num
array pointed by r8
. The length of the input string is 17 bytes since it can read one 64-bit hexadecimal number (16 bytes) plus a new line character (1 byte).
After each input, we increment the r8
register by 17 bytes to point to the next position in the num
array. We decrement the cnt
variable by one to keep track of the number of remaining inputs. The loop runs 5 times to input all the 5 numbers.
OutputMsg:
mov rax,01
mov rdi,01
mov rsi,msg3
mov rdx,len3
syscall
This section prints the message indicating that the program is going to display the 5 numbers.
Please Like the blog if you like it or find helpfull !!
Subscribe to my newsletter
Read articles from Ayush Bulbule directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ayush Bulbule
Ayush Bulbule
Hello! I'm an aspiring software engineer with a strong foundation in computer engineering. Passionate about developing scalable, efficient solutions and enhancing application functionality. Experienced in full-stack development, cloud computing, and security. Always eager to learn and contribute to innovative tech projects. Let's connect and collaborate!