Why can't I print the user input data in NASM assembly? - linux

I am trying to verify the length of user input in this example (NASM):
section .bss
user_input resb 10
section .text
push ebp
mov ebp, esp
mov eax, 3 ;sys_read
mov ebx, 0 ;stdin
mov ecx, user_input
mov edx, 10
int 80h
cmp eax, 10
jg overflow
jmp done
overflow:
.
.
.
done:
mov eax, 1
int 80h
Why isn't this working?

Since your buffer is 10 bytes and you put the length into edx, the return can never be greater than 10 and you jump to label done: and exit.
There's nothing in the code you've shown that prints anything out.

Related

Assembly language taking input from user but displaying output

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

How can i compare the user input with some constant 1, 2 like a switch case in assembly (NASM)?

In the question below, i saw how to do a compare between user input and some option specified by the programmer.
Calculator in Assembly Language - Linux x86 & NASM - Division
But in my case isn't work.
Here is the peace of my code.
section .data
msgTes db 'NEW ELEMENT'
msgTes_len equ $-msgTes
section .bss
opt resb 2
.
.
.
new_element: ; option one is a new element
push ebp
mov ebp, esp
mov eax, 4
mov ebx, 1
mov ecx, msgTes
mov edx, msgTes_len
int 80h
pop ebp
ret
read_option:
push ebp
mov ebp, esp
mov eax, 3 ; reading the option
mov ebx, 0
mov ecx, opt
mov edx, 2
int 80h
mov ah, [opt] ; which option the user entered?
sub ah, '0'
cmp ah, 1
je new_element
pop ebp
ret
There is a menu with the options, but is irrelevant for the question.
Why the message "NEW ELEMENT" isn't printed?

x86 Intel Assembly Linux sys_write + sys_read

The following code:
section .bss
name: resb 50
section .text
global _start
_start:
PUSH EBP
MOV EBP, ESP
MOV EDX, len
MOV ECX, msg
MOV EBX, 1
MOV EAX, 4
INT 0x80
MOV EDX, 50
MOV ECX, name
MOV EBX, 0
MOV EAX, 3
INT 0x80
MOV EBX, 1
MOV EAX, 4
INT 0x80
MOV EDX, cm
MOV ECX, ex
MOV EBX, 1
MOV EAX, 4
INT 0x80
MOV EBX, 0
MOV EAX, 1
INT 0x80
section .data
msg db 'Hello!',0xa
ex db '!',0xa
len equ $ - msg
cm equ $ - ex
I intended to make a simple I/O program that printed Hello!, asked for a char and would print %c!.
Input being | and output being :, I get the following:
:Hello!
:!
|4
:4
:!
How do I make it so that it returns the following
:Hello!
|4
:4!
As Damien_The_Unbeliever says, your equs want to come immediately after the string they're supposed to measure. After your sys_read, eax will be the number of characters read, including the linefeed that ends the reading. You probably don't want to print the linefeed (in this case - sometimes you would). So:
mov edx, eax
dec edx
Or if you want to do it in one instruction:
lea edx, [eax - 1]
As it stands, edx still holds 50, so your next sys_write will print 50 characters. It will NOT stop at a zero or any other string-terminator. ecx will still contain name, but I would reload it just for clarity.
By rights, you should check for an error return (eax would be negative) after each and every int 0x80 but an error is unlikely here.

NASM loop over bytes

Currently I'm trying to loop over every single byte in a buffer (read from a file) and compare it to see if any of them is a whitespace, and write them to STDOUT. For some reason the program compiles and runs fine, but produces zero output.
section .data
bufsize dw 1024
section .bss
buf resb 1024
section .text
global _start
_start:
; open the file provided form cli in read mode
mov edi, 0
pop ebx
pop ebx
pop ebx
mov eax, 5
mov ecx, 0
int 80h
; write the contents in to the buffer 'buf'
mov eax, 3
mov ebx, eax
mov ecx, buf
mov edx, bufsize
int 80h
; write the value at buf+edi to STDOUT
mov eax, 4
mov ebx, 1
mov ecx, [buf+edi]
mov edx, 1
int 80h
; if not equal to whitespace, jump to the loop
cmp byte [buf+edi], 0x20
jne loop
loop:
; increment the loop counter
add edi, 1
mov eax, 4
mov ebx, 1
mov ecx, [buf+edi]
int 80h
; compare the value at buf+edi with the HEX for whitespace
cmp byte [buf+edi], 0x20
jne loop
; exit the program
mov eax, 1
mov ebx, 0
int 80h
The main problem was that I didn't given the address of bufsize ([bufsize]), also the loops had some problems.
Here's the fixed version, thanks everyone for your input.
section .data
bufsize dd 1024
section .bss
buf: resb 1024
section .text
global _start
_start:
; open the file provided form cli in read mode
mov edi, 0
pop ebx
pop ebx
pop ebx
mov eax, 5
mov ecx, 0
int 80h
; write the contents in to the buffer 'buf'
mov eax, 3
mov ebx, eax
mov ecx, buf
mov edx, [bufsize]
int 80h
; write the value at buf+edi to STDOUT
; if equal to whitespace, done
loop:
cmp byte [buf+edi], 0x20
je done
mov eax, 4
mov ebx, 1
lea ecx, [buf+edi]
mov edx, 1
int 80h
; increment the loop counter
add edi, 1
jmp loop
done:
; exit the program
mov eax, 1
mov ebx, 0
int 80h

Loop/Input Logic Flow Issue (NASM x86 Assembly)

I have a program below that tries to take input from the user and repeat that same string until the user enters it again. (It's a personal learning project)
However, I am having some severe diffuculty in getting it to perform correctly. In a past thread here, you can see the input, pun intended, that other users have provided on this problem.
%include "system.inc"
section .data
greet: db 'Hello!', 0Ah, 'Please enter a word or character:', 0Ah
greetL: equ $-greet ;length of string
inform: db 'I will now repeat this until you type it back to me.', 0Ah
informL: equ $-inform
finish: db 'Good bye!', 0Ah
finishL: equ $-finish
newline: db 0Ah
newlineL: equ $-newline
section .bss
input: resb 40 ;first input buffer
check: resb 40 ;second input buffer
section .text
global _start
_start:
greeting:
mov eax, 4
mov ebx, 1
mov ecx, greet
mov edx, greetL
sys.write
getword:
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 40
sys.read
sub eax, 1 ;remove the newline
push eax ;store length for later
instruct:
mov eax, 4
mov ebx, 1
mov ecx, inform
mov edx, informL
sys.write
pop edx ;pop length into edx
mov ecx, edx ;copy into ecx
push ecx ;store ecx again (needed multiple times)
mov eax, 4
mov ebx, 1
mov ecx, input
sys.write
mov eax, 4 ;print newline
mov ebx, 1
mov ecx, newline
mov edx, newlineL
sys.write
mov eax, 3 ;get the user's word
mov ebx, 0
mov ecx, check
mov edx, 40
sys.read
sub eax, 1
push eax
xor eax, eax
checker:
pop ecx ;length of check
pop ebx ;length of input
mov edx, ebx ;copy
cmp ebx, ecx ;see if input was the same as before
jne loop ;if not the same go to input again
mov ebx, check
mov ecx, input
secondcheck:
mov dl, [ebx]
cmp dl, [ecx]
jne loop
inc ebx
inc ecx
dec eax
jnz secondcheck
jmp done
loop:
pop edx
mov ecx, edx
push ecx
mov eax, 4
mov ebx, 1
mov ecx, check
sys.write ;repeat the word
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, newlineL
sys.write
mov eax, 3 ;replace new input with old
mov ebx, 0
mov ecx, check
mov edx, 40
sys.read
jmp checker
done:
mov eax, 1
mov ebx, 0
sys.exit
Example output would yield:
Hello!
Please enter a word or character:
INPUT: Nick
I will now repeat this until you type it back to me.
Nick
INPUT: Nick
N
INPUT: Nick
INPUT: Nick
And that goes on forever until is ^C it to death. Any ideas on the problem?
Thanks.
instruct leaves two items on the stack, which are consumed by checker the first time round the loop. But they are not replaced for the case where you go round the loop again. This is the most fundamental problem in your code (there may be others).
You could find this by running with a debugger and watching the stack pointer esp; but it can be seen just by looking at the code -- if you take everything out except for the stack manipulation and branches, you can clearly see that the checker -> loop -> back to checker path pops three items but only pushes one:
greeting:
...
getword:
...
push eax ;store length for later
instruct:
...
pop edx ;pop length into edx
...
push ecx ;store ecx again (needed multiple times)
...
push eax
checker:
pop ecx ;length of check
pop ebx ;length of input
...
jne loop ;if not the same go to input again
...
secondcheck:
...
jne loop
...
jnz secondcheck
jmp done
loop:
pop edx
...
push ecx
...
jmp checker
done:
...
There are better ways to keep long-lived variables than trying to shuffle them around on the stack like this with push and pop.
Keep them in a data section (the .bss you already have would be suitable) instead of on the stack.
Allocate some space on the stack, and load/store them there directly. e.g. sub esp, 8 to reserve two 32-bit words, then access [esp] and [esp+4]. (The stack should be aligned to a 32-bit boundary, so always reserve a multiple of 4 bytes.) Remember to add esp, 8 when you've finished using it.
(These are essentially the equivalent of what a C compiler would do for global (or static) variables, and local variables, respectively.)

Resources