Assembly language taking input from user but displaying output - linux

I wrote this code, it reads the data from user but did not display the output. It is written in Assembly language. I am new to Assembly language. Can somebody please help me in solving this. I shall be very thankful. Thanks in advance. Here is the code:
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

In typical environments, file descripter 0 stands for standard input, 1 for standard output, and 2 for standard error output.
Reading from standard error output makes no sense for me.
Try changing the program for reading
;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
to
;Read and store the user input
mov eax, 3
mov ebx, 0
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
in order to have the system read some data from standard input.

section .data
out1: db 'Enter the number:'
out1l: equ $-out1
out2: db 'The number you entered was:'
out2l: equ $-out2
section .bss
input: resb 4
section .text
global _start
_start:
;for displaying the message
mov eax,4
mov ebx,1
mov ecx,out1
mov edx,out1l
int 80h
;for taking the input from the user
mov eax,3
mov ebx,0
mov ecx,input
mov edx,4
int 80h
;for displaying the message
mov eax,4
mov ebx,1
mov ecx,out2
mov edx,out2l
int 80h
;for displaying the input
mov eax,4
mov ebx,1
mov ecx,input
mov edx,4
int 80h
mov eax,1
mov ebx,100
int 80h

Related

Invalid combination of opcode and operands in program assembly

At start I want to say that I'm a beginner in Assembly. I want to write a program which firstly adds two numbers and then divide the result by 2, so i want to get an average of two numbers. The problem is in section after dividing, but program without this section about dividing works well and prints the sum.
section .data
mess1 db 'Podaj pierwsza liczbe: '
len1 equ $- mess1
mess2 db 'Podaj druga liczbe: '
len2 equ $- mess2
mess3 db 'Wynik: '
len3 equ $- mess3
section .bss
zmienna1 resb 4
zmienna2 resb 4
wynik resb 8 ;result
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,mess1
mov edx,len1
int 0x80
mov eax,3 ;sys_read to 3
mov ebx,0 ;stdin
mov ecx,zmienna1
mov edx,4 ;4 to rozmiar
int 0x80
mov eax,4
mov ebx,1
mov ecx,mess2
mov edx,len2
int 0x80
mov eax,3
mov ebx,0 ;sys_read i stdin
mov ecx,zmienna2
mov edx,4 ;4 rozmiar
int 0x80
mov eax,4
mov ebx,1
mov ecx,mess3
mov edx,len3
int 0x80
;Teraz wrzuc zmienna1 do eax, a zmienna2 do ebx
; odejmij ASCII-owe '0' aby przekonwertowac na dzisiejtne
mov eax,[zmienna1]
sub eax,'0'
mov ebx,[zmienna2]
sub ebx,'0'
add eax,ebx
add eax,'0' ;zamien na binarne z decymalnego
mov [wynik],eax
div wynik,'2'
add wynik,'0'
;pokaz sume
mov eax,4 ;
mov ebx,1
mov ecx,wynik
mov edx,8 ;8 to rozmiar tego wyniku z bss
int 0x80
exit:
mov eax,4
xor ebx,ebx
int 0x80

NASM unexpected output when printing

I am new have very small problem with assembly NASM in linux. I made simple program for practice that when you put in the text, it adds simple decoration in form of stars. The expected output is:
*********EXAMPLE*********
instead:
*********EXAMPLE
*********
here is the complete code of the program (long) i have use edb to check the code and check EDX register if it match the len take by the null byte check to print correct number of characters.
section .data
prompt db "Please enter a word (MAX: 10 Characters) : ", 0xa, 0xd
plen equ $ - prompt
stars times 9 db "*"
section .bss
text resb 10
section .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, prompt
mov edx, plen
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, text
mov edx, 11
int 0x80
xor ecx, ecx
mov esi, text
mov ecx, 0
loop1:
inc ecx
cmp byte [esi + ecx], 0x00
jne loop1
push ecx
jmp printexit
printexit:
mov eax, 4
mov ebx, 1
mov ecx, stars
mov edx, 9
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, text
pop edx
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, stars
mov edx, 9
int 0x80
mov eax, 1
int 0x80

Behavior of read syscall in Assembly (ASM)

I am currently learning the ASM, I have a question about the following code (which compiles)
This code come from this tutorial.
The question is: Why do I have the same behaviour when the fd is egual to 0, 1 or 2 (corresponding to stdin, stdout and stderr), at the indicated line, and when the fd is egual to 3 or more, it does nothing (it skips the scanf).
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 ; /!\ QUESTION IS ABOUT THIS LINE /!\
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
We can compile and execute this code with the following command:
$> nasm -f elf64 test.S
$> ld test.o
$> ./a.out
Thank you,

Division in assembly program

this is my code :
section .bss ;Uninitialized data
average resb 5
num1 resb 5
num2 resb 5
num3 resb 5
section .text
global _start ; make the main function externally visible
_start:
;User prompt
mov eax, 4
mov ebx, 1
mov ecx, messaging
mov edx, lenmessaging
int 0x80
;Read and store the user input
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, messaging
mov edx, lenmessaging
int 0x80
;Read and store the user input
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 0x80
;Read and store the user input
mov eax, 3
mov ebx, 0
mov ecx, num3
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 0x80
;Output the message
mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 0x80
; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
mov eax, [num1]
sub eax, '0'
mov ebx, [num2]
sub ebx, '0'
; add eax and ebx
add eax, ebx
mov ebx, [num3]
sub ebx, '0'
add eax, ebx
mov bl, '3'
sub bl, '0'
div bl
add eax, '0'
mov [average], eax
;Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, average
mov edx, 5
int 0x80
;////////////////
mov eax, 0x1 ; system call number for exit
sub esp, 4 ; OS X (and BSD) system calls needs "extra space" on stack
int 0x80 ; make the system call
section .data ;Data segment
userMsg db 'Please enter a number: ' ;Ask the user to enter a nu
lenUserMsg equ $-userMsg ;The length of the message
dispMsg db 'The average is : '
lenDispMsg equ $-dispMsg
messaging db 'Enter another number : '
lenmessaging equ $-messaging
when I run it ,I have this error :
how can I fix it?
this program is for calculating the average of three numbers.
thanks ;)

Problems with multiple 2-digit inputs in Assembly

I'm fairly new to this PL and I really don't know what I'm doing. It seems my code just takes the first two-digit number and not the second or third.
All I really do is print a prompt asking for a two-digit number, as you can see below. Then it takes the tens and ones digit. Can someone point out what's wrong?
;Prompt to enter first number
...
;Gets the first number
mov eax, 3
mov ebx, 0
mov ecx, num1a
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, num1b
mov edx, 1
int 80h
;Prompt to enter Second Number
...
;Gets the second number
mov eax, 3
mov ebx, 0
mov ecx, num2a
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, num2b
;mov edx, 1
int 80h
;Prompt to enter Third Number
...
;Gets the third number
mov eax, 3
mov ebx, 0
mov ecx, num3a
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, num3b
mov edx, 1
int 80h
It wont let me put anymore code, but the ... in the codes are all the same.
mov eax, 4
mov ebx, 1
mov ecx. prompt
mov edx, promptLen
int 80h
where:
section .data
prompt db 'Enter a two-digit number: '
promptLen equ $-prompt
What you can do is, increase the size of edx when you are reading the value from the user.
mov eax, 3
mov ebx, 0
mov ecx, num1b
mov edx, 32
int 80h

Resources