Simple hello world aseembly program prints out trash - linux

I am using a x86 assembly to make a simple program that compares two words and prints out if they are equal (i know its usless im just learning and testing out things) its working properly but right after the answer i get printed out with alot of garbage and i dont understand what is causing this bug
SECTION .bss
SECTION .text
SECTION .data
HelloMsg: db "Helllo",10
HelloLength: equ $-HelloMsg
HellloMsg: db "Helllo",10
HellloLength: equ $-HellloMsg
One: db "First",10
OneLen: equ $-One
Two: db "Second",10
TwoLen: equ $-Two
global _start
_start:
nop
mov eax,4
mov ebx,1
mov ecx,HellloLength
mov edx,HelloLength
cmp ecx,edx
je true
mov ecx,One
mov edx,OneLen
int 80H
true:
mov ecx,Two
mov edx,One
int 80H
MOV eax,1
mov ebx,0
int 80H
Output :
Sorry if im asking a stupid question or if my program is hard to read

There is a small bug in the code. When calling the write system call edx must contain the length of the string to be written. However, in the case that the comparison strings have the same length (the true: case), your code is:
true:
mov ecx,Two
mov edx,One
int 80H
but One refers to the string "First", not the length of string Two.
Fix it by changing it to:
true:
mov ecx,Two
mov edx,TwoLen
int 80H

Related

NASM - How can I solve the input reading problem from the terminal? [duplicate]

This question already has answers here:
Read STDIN using syscall READ in Linux: unconsumed input is sent to bash
(2 answers)
Closed 4 months ago.
section .data
yourinputis db "your input is =",0
len equ $ - yourinputis
section .bss
msginput resb 10
section .text
global _start
_start:
mov eax,3 ;read syscall
mov ebx,2
mov ecx,msginput
mov edx,9 ; I don't know that is correct?
int 80h
mov eax,4 ;write syscall
mov ebx,1
mov ecx,yourinputis
mov edx,len
int 80h
mov eax,4 ;write syscall
mov ebx,1
mov ecx,msginput
mov edx,10
int 80h
exit:
mov eax,1 ;exit syscall
xor ebx,ebx
int 80h
This code working very well. But It is so terrible bug(for me:(). If I enter an input longer than 10 --->
$./mycode
012345678rm mycode
your input is 012345678$rm mycode
$
This is happening. And of course "mycode" is not exist right now.
What should I do?
EDIT:The entered input is correctly printed on the screen. But if you enter a long input, it moves after the 9th character to the shell and runs it.
In the example, the "rm mycode" after "012345678" is running in the shell.
If you enter more than 9 characters, they're left in the terminal driver's input buffer. When the program exits, the shell reads from the terminal and tries to execute the rest of the line as a command.
To prevent this, your program should keep reading in a loop until it gets a newline.
You can read the characters one by one until you reach 0x0a. Something like:
_read:
mov esi, msginput
_loop:
mov eax,3 ;read syscall
mov ebx,0
mov ecx, esi
mov edx,1 ; I don't know that is correct?
int 80h
cmp byte[esi], 0x0a
je end
inc esi
jmp _loop
end:
ret
You would have to increase the size of msginput tho.
IMPORTANT: Do note that this is not the efficient way to do this (see the comments), it is only put here as an example to the answer above.

A loop in assembly doesn't work why?

i have problem. I tried build a loop in assembly (nasm,linux). The loop should "cout" number 0 - 10, but it not work and i don't know why. Here is a code :
section .text
global _start
_start:
xor esi,esi
_ccout:
cmp esi,10
jnl _end
inc esi
mov eax,4
mov ebx,1
mov ecx,esi
mov edx,2
int 80h
jmp _ccout
_end:
mov eax,1
int 80h
section .data
Well, the loop is working, but you aren't using the syscall correctly. There are some magic numbers involved here, so let's get that out of the way first:
4 is the syscall number for write
1 is the file descriptor for the standard output
So far, so good. write requires a file descriptor, the address of a buffer and the length of that buffer or the part of it that it's supposed to write to the file descriptor. So, the way this is supposed to look is similar to
mov eax,4 ; write syscall
mov ebx,1 ; stdout
mov ecx,somewhere_in_memory ; buffer
mov edx,1 ; one byte at a time
compare that to your code:
mov eax,4
mov ebx,1
mov ecx,esi ; <-- particularly here
mov edx,2
int 80h
What you are doing there (apart from passing the wrong length) is passing the contents of esi to write as a memory address from which to read the stuff it's supposed to write to stdout. By pure happenstance this doesn't crash, but there's no useful data at that position in memory.
In order to solve this, you will need a location in memory to put it. Moreover, since write works on characters, not numbers, you'll have to to the formatting yourself by adding '0' (which is 48 in ASCII). All in all, it could look something like this:
section .data
text db 0 ; text is a byte in memory
section .text
global _start
_start:
xor esi,esi
_ccout:
cmp esi,10
jnl _end
inc esi
lea eax,['0'+esi] ; print '0' + esi. lea == load effective address
mov [text],al ; is useful here even though we're not really working on addresses
mov eax,4 ; write
mov ebx,1 ; to fd 1 (stdout)
mov ecx,text ; from address text
mov edx,1 ; 1 byte
int 80h
jmp _ccout
_end:
mov [text],byte 10 ; 10 == newline
mov eax,4 ; write that
mov ebx,1 ; like before.
mov ecx,text
mov edx,1
int 80h
mov eax,1
mov ebx,0
int 80h
The output 123456789: is probably not exactly what you want, but you should be able to take it from here. Exercise for the reader and all that.

Assembly code doesn't print string

So basically I'm trying to write a hello world program in assembly. The program exits as it should but no string is printed along the way. There are no errors anywhere either. I suspect that I am declaring or using the string wrong somehow.
.intel_syntax noprefix
.data
msg:
.ascii "Hello World"
.text
.globl _start
_start:
mov eax, 4 #call write
mov ebx, 1 #output into stdout
mov ecx, msg #what to write
mov edx, 11 #length of what to write
int 0x80
mov eax, 1 #exit
mov ebx, 0
int 0x80
I have also tried replacing
mov ecx, msg
with
mov ecx, [msg]
but it doesn't seem to make a difference.
You need to use mov ecx, offset msg or lea ecx, msg.
Also make sure you are assembling as 32 bit code in case you are on a 64 bit system.

Printing variable to command line using assembly in Linux

Trying my hand at Linux assembly and I'm running into the following problem. I'm just starting out so my program is a relatively simple one derived from some examples I found over at linuxassembly. It takes the first argument passed to the command line and prints it out. Here is what I have so far...
section .bss
test_string: resb 3
section .text
global _start
_start:
pop ebx ;argument number
pop ebx ;program name
pop ebx ;first argument
mov [test_string],ebx
mov eax,4
mov ebx,1
mov ecx,test_string
mov edx,3
int 80h
mov eax,1
mov ebx,0
int 80h
I know that this is poorly written, but since I'm new to this, I'm just looking to better understand how assembly instructions/variables work before I move on. I assemble and link using...
nasm -f elf first.asm
ld -m elf_i386 -s -o first first.o
Then I run using..
./first one two
I was thinking that it would print out one but it prints out gibberish like Y*&. What am I doing wrong? Is my test_string the wrong type?
You're trying to print out the value of the pointer to the string instead of printing the string. You want to do this instead.
pop ebx ;argument number
pop ebx ;program name
pop ebx ;pointer to the first argument
mov ecx,ebx ;load the pointer into ecx for the write system call
mov eax,4 ;load the other registers for the write system call
mov ebx,1
mov edx,3
int 80h
mov eax,1
mov ebx,0
int 80h

Nasm program segmentation fault

I am trying to understand the stack in nasm better, so I made this program to try to pass "arguments" to a "function" in nasm. I am very new to this assembly.
section .data
v0s0msg0: db 'Enter something',10
v1t0msg0L: equ $-v0s0msg0
section .bss
v2i0inp0 resb 256
v3v0temp0 resb 256
section .text
global _start
_start:
;This is a nasm program to help me understand the stack better
mov eax,4
mov ebx,1
mov ecx,v0s0msg0
mov edx,v1t0msg0L
int 80h
mov eax,3
mov ebx,0
mov ecx,v2i0inp0
mov edx,256
int 80h
push dword v2i0inp0
call f0m0test0
mov eax,1
mov ebx,0
int 80h
f0m0test0:
pop dword[v3v0temp0]
mov eax,4
mov ebx,1
mov ecx,v3v0temp0
mov edx,256
int 80h
ret 4
I can assemble it, link it, and run it just fine, but when running it, after I enter the input, it just says segmentation fault following two '?' looking characters.
I've tried changing
pop dword[v3v0temp0]
to something like:
pop v3v0temp0
or even:
mov v3v0temp0,dword[ebp]
and many things like that, but they all end up as either segmentation faults, or as an error in the assembler saying:
invalid combination of opcode and operands
I would really appreciate help to make this program work, also, please explain a little bit about the stack, using the prefix 'dword', and what the '[]' characters are for. I would like an explanation just of how to use the stack for "arguments".
I am running this on a linux os, Ubuntu
Thank you in advance
f0m0test0:
pop dword[v3v0temp0]
This pops the return address off the stack, not the parameter.
mov eax,4
mov ebx,1
mov ecx,v3v0temp0
mov edx,256
int 80h
ret 4
Since you've already poped something (though not the intended parameter) off stack, ret 4 above is almost definitely wrong.
I think you want just:
f0m0test0:
mov eax,4
mov ebx,1
mov ecx,[esp+4]
mov edx,256
int 80h
ret 4
Alternatively, instead of the callee cleaning up the parameter with ret 4, have the caller do it (which, I believe, is the usual calling convention):
push dword v2i0inp0
call f0m0test0
add esp,4

Resources