Printing variable to command line using assembly in Linux - 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

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.

Print character using linux x86 assembly and at&t syntax [duplicate]

In DOS Assembly we can do this:
mov dl, 41h
mov ah, 02h
int 21h
But how about Linux nasm x86 Assembly?
section .data
msg db 'H'
len equ $ - msg
section .text
global _start
_start:
mov edx,len
mov ecx,msg
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80
mov eax,1 ;system call number (sys_exit)
int 0x80
Writing a single character may not produce the desired output, because depending on the terminal settings, it may be cached, so you may need to flush the output, to make sure that it appears wherever you write to.
Here is a list of linux 32 Bit system calls.

Assembler x86 without stdlib

I want to read command line arguments when an assembly program is run. Do I have to use sys_read or something else?
I know how to access data using things like scanf. For instance:
mov eax, 3
mov ebx, 1
mov ecx, msg
mov edx len
int 0x80
What I'm looking for, though, are the command line arguments. When I execute ./a.out 45 23 I want to get access to the '45' for example.
And so, if anybody have a page where the unixstd.h defined... I would be grateful.
Accessing command line arguments under ELF32 Linux is available through the stack:
mov eax, [esp+8]
mov ebx, [eax]
mov ecx, offset msg
mov [ecx], ebx
Credit to #Jester for his answer in the comments!

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.

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