strange assembly goof-up involving a working, but incorrect program
GNU nano 2.2.4 File: argv.s
.section .text
.globl _start
_start:
movl %esp, %ebp
movl 8(%ebp),%ecx
movl $4,%edx
movl $1,%ebx
movl $4,%eax
int $0x80
movl $1,%eax
movl $0,%ebx
int $0x80
let me preface this question by saying that i'm (obviously) new to asm and any random general advice not related to the topic is welcome... I meant to simply read in the argv[1] from the bash termina开发者_运维问答l and print it back out. This was intended to be a test to ensure that I actually knew how to take arguments. If I enter an integer, like 2. The program prints 2O. capital 'o', not zero. why?
You need to understand that argv[1] is not an integer as you would like, but a string terminated null instead! A string is sequence of bytes ending with '\0'
.
This means that when you pass the character 2 through the cmd-line to your app, this string in memory will look like [2][\0]
, which consists of only 2 bytes. The problem is that you specify the size in %edx as being 4, which is not true, and will make write()
print another 2 bytes of memory garbage to your screen.
The right way to do what you want is implement the strlen()
function on your application. This function iterates through the stream of bytes counting how many bytes there are before the character \0
shows up. Only then you'll be able to print the correct string. Use the number returned by strlen()
as %edx when you write()
.
精彩评论