input_output.asm
. Here we would also require a bss section to store
the unintialized data variable to be taken from user.
global _start ; must be declared for linker
section .data
input_prompt db "Enter an integer " ; input_prompt="Enter an integer "
len_input_prompt equ $ - input_prompt ; len_input_prompt equals size of input_prompt
disp_prompt db "You entered " ; disp_prompt="You entered "
len_disp_prompt equ $ - disp_prompt ; len_disp_prompt equals size of disp_prompt
section .bss
data resb 5 ; Unitialized data variable data
section .text
_start: ; start label
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, input_prompt ; ecx=input_prompt
mov edx, len_input_prompt ; edx=len_input_prompt
int 0x80 ; Calling interrupt handler
mov eax, 3 ; sys_read system call
mov ebx, 2 ; stdin file descriptor
mov ecx, data ; Read input value in data
mov edx, 5 ; 5 bytes (numeric, 1 for sign) of that data value
int 0x80 ; Calling interrupt handler
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, disp_prompt ; ecx=disp_prompt
mov edx, len_disp_prompt ; edx=len_disp_prompt
int 0x80 ; Perform the system call
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, data ; ecx=data
mov edx, 5 ; edx=5bytes for length of data
int 0x80 ; Perform the system call
mov eax , 1 ; sys_exit system call
mov ebx , 0 ; setting exit status
int 0x80 ; Calling interrupt handler to exit program
>> nasm -f elf32 input_output.asm -o input_output.o
>> ld -m elf_i386 input_output.o -o input_output
>> ./input_output
>> echo $?
>> Enter an integer 15
>> You entered 15
>> 0