Writing Assembly Program to Input a Number and Print it.
Assembly Tutorial 02
Table of contents
No headings in the article.
Hola reader! This is the second blog of our assembly tutorial series. Today we will see how we can read a hex number and print it on screen.
global _start
section .data
msg1 db "Program to read a Number and Display it.", 0xA
len1 equ $ - msg1
msgIn db "Enter a number = ", 0xA
msgInLen equ $ - msgIn
msgOut db "You Entered = ", 0xA
msgOutLen equ $ - msgOut
section .bss
num resb 2
section .text
_start:
MSG1: ; prinit msg
mov rax, 01
mov rdi, 01
mov rsi, msg1
mov rdx, len1
syscall
; print msg1
mov rax, 01
mov rdi, 01
mov rsi, msgIn
mov rdx, msgInLen
syscall
mov r8, num ; pointer to variable
; take input
mov rax, 00
mov rdi, 00
mov rsi, r8
mov rdx, 16
syscall
; print msg2
mov rax, 01
mov rdi, 01
mov rsi, msgOut
mov rdx, msgOutLen
syscall
Output_NUM: ; print numbers
mov rax, 01
mov rdi, 01
mov rsi, r8
mov rdx, 16
syscall
Exit: ; exit
mov rax, 60
mov rdx, 00
syscall
Explanation :
global _start
This line declares the label "_start" as the entry point of the program. It also makes the program visible to other programs and allows it to be called from outside the program.
section .data
msg1 db "Program to read a Number and Display it.", 0xA
len1 equ $ - msg1
msgIn db "Enter a number = ", 0xA
msgInLen equ $ - msgIn
msgOut db "You Entered = ", 0xA
msgOutLen equ $ - msgOut
This section defines the program data. The ".data" section contains three messages defined using the "db" (define byte) directive. The messages are stored as ASCII characters and are terminated with a newline character (0xA). The "len1", "msgInLen", and "msgOutLen" labels are used to calculate the length of each message.
section .bss
num resb 2
This section defines the uninitialized data that the program will use. The ".bss" section reserves 2 bytes of memory for the "num" variable.
section .text
_start:
This section contains the program's code. The ".text" section holds the code instructions that the CPU will execute. The "_start" label specifies where the program execution starts.
MSG1: ; prinit msg
mov rax, 01
mov rdi, 01
mov rsi, msg1
mov rdx, len1
syscall
This section prints the "msg1" message to the console. It uses the "mov" instruction to load the appropriate values into the registers and the "syscall" instruction to invoke the operating system to print the message to the console.
; print msgIn - Enter a number =
mov rax, 01
mov rdi, 01
mov rsi, msgIn
mov rdx, msgInLen
syscall
This section prompts the user to enter a number by printing the "msgIn" message to the console. It uses the "mov" and "syscall" instructions similar to the previous section.
mov r8, num ; pointer to variable
This section loads the memory address of the "num" variable into the "r8" register. This register will be used later to read user input.
; take input number from user
mov rax, 00
mov rdi, 00
mov rsi, r8
mov rdx, 16
syscall
This section reads user input from the console and stores it in the "num" variable. It uses the "mov" and "syscall" instructions. The "rax" register contains the system call number for "read", the "rdi" register specifies the file descriptor (0 for standard input), the "rsi" register contains the memory address of the "num" variable, and the "rdx" register specifies the maximum number of bytes to read.
msg2: ; print msg2 - You Entered =
mov rax, 01
mov rdi, 01
mov rsi, msgOut
mov rdx, msgOutLen
syscall
This section prints the "msgOut" message to the console.
Output_NUM: ; print numbers
mov rax, 01
mov rdi, 01
mov rsi, r8
mov rdx, 16
syscall
This section prints the value of the "num" variable to the console. It uses the same "mov" and "syscall" instructions as the previous sections, except that it now uses the "rsi" register to specify the memory address of the "num" variable.
Exit: ; exit
mov rax, 60
mov rdx, 00
syscall
This section exits the program by calling the "exit" system call. It loads the "rax" register with the system call number for "exit", sets the "rdx" register to 0, and calls the operating system to terminate the program.
Overall, this program prompts the user to enter a number, reads the input, and displays it back to the user. The program uses system calls to interact with the operating system and read and write data to the console.