; ------------------------------------------------------------------
; userinput.asm
;
; This is a Win32 console program that reads user input from the keyboard
; and displays it on the screen then exits.
;
; To assemble to .obj: nasm -f win32 userinput.asm
; To compile to .exe: gcc userinput.obj -o userinput.exe
; ------------------------------------------------------------------
global _main ; _start
; The data section is used for declaring initialized data or constants.
; This data does not change at runtime.
; You can declare various constant values, file names or buffer size etc. in this section.
section .data
inputMsg db 'Please enter a number: ', ; Ask the user to enter a number.
inputMsglen equ $-inputMsg ; length of the input message.
outputMsg db 'The entered number is: '
outputMsgLenm equ $-outputMsg
; The bss section is used for declaring variables.
section .bss
num resb 5
; The text section is used for keeping the actual code.
; This section must begin with the declarationglobal main,
; which tells the kernel where the program execution begins.
section .text
; _start:
_main:
; User prompt "Please enter a number: "
mov edx, inputMsglen ; message length
mov ecx, inputMsg ; message to write
mov ebx, 01h ; file descriptor (stdout)
mov eax, 04h ; system call number (sys_write)
int 0x80 ; call kernel
; Read & store user input.
mov edx, 5 ; 5 bytes length (numeric, 1 for sign) of that information
mov ecx, num ; variable to store user input
mov ebx, 02h ; file descriptor (stdin)
mov eax, 03h ; system call number (sys_read)
int 0x80 ; call kernel
; Output the message 'The entered number is: '
mov edx, outputMsgLenm ; message length
mov ecx, outputMsg ; message to write
mov ebx, 01h ; file descriptor (stdout)
mov eax, 04h ; system call number (sys_write)
int 0x80 ; call kernel
; Output the number entered
mov edx, 5 ;
mov ecx, num ;
mov ebx, 01h ;
mov eax, 04h ;
int 80h ;
jmp exit
exit:
mov eax, 01h ; exit()
mov ebx, 0 ; errno
int 80h
OyAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KOyB1c2VyaW5wdXQuYXNtCjsKOyBUaGlzIGlzIGEgV2luMzIgY29uc29sZSBwcm9ncmFtIHRoYXQgcmVhZHMgdXNlciBpbnB1dCBmcm9tIHRoZSBrZXlib2FyZAo7IGFuZCBkaXNwbGF5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