I'm trying to learn system calls in assembly from this page on tutorialspoint.
On that page, there is an assembly code that reads user input and prompts it out. Which looks confusing to me and doesn't work unfortunately:
section .data ;Data segment
userMsg db 'Please enter a number: ' ;Ask the user to enter a number
lenUserMsg equ $-userMsg ;The length of the message
dispMsg db 'You have entered: '
lenDispMsg equ $-dispMsg
section .bss ;Uninitialized data
num resb 5
section .text ;Code Segment
global _start
_start: ;User prompt
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
;Output the message 'The entered number is: '
mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 80h
;Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 5
int 80h
; Exit code
mov eax, 1
mov ebx, 0
int 80h
Once the code is executed, the program never asks for input - it calls system exit immediately.
I find certain parts of the code confusing, and assume that they might have to do something with failure:
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
On the code above, for eax
(32-bit accumulator register) it makes sense to be 3
, since it performs sys_read
system call. edx
probably defined data type, and considering that we are saving integer, 5
makes sense.
But 32-bit base register should contain file descriptor index (where stdin=0
, stdout=1
, stderr=2
). But why is ebx=2
in the code above?
Apologies if the question is too simple, but why wouldn't the code work? Is there something wrong with incorrect choices of inputs in registers? i.e what I mentioned above.