; ------------------------------------------------------------------
; 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
