Question

I am new to assembly language and am facing a problem in my first steps. I have some experience with high level languages but I feel like a total beginner in this low level.

I am using the GNU assembler (gas) version 2.23 x86_64 on a Xubuntu system.

I wrote a simple hello world and now I want to extend it to following features:

  • read input using syscall 3
  • check if input is greater then a specified
  • if shorter or equal print the input
  • else print an error message

If I run the code it works for the shorter input, but if I type in more the 5 characters, I get an access violation.

I work with this (German) tutorial.

Here is my code:

#
# author: linluk
# filename: name.s
#
# usage:
#   $ as name.s -o name.o
#   $ ld name.o -o name
#   $ ./name
# 

.section .data
  hello: .ascii "hello, what is your name?\n"
  err: .ascii "too long!\n"
  name: .ascii ""

.section .text

.globl _start

_start:
  #output
  mov $4, %eax      # 4 syscall write
  mov $1, %ebx      # 1 filedescriptor stdout
  mov $hello, %ecx  # output buffer
  mov $26, %edx     # length of buffer
  int $0x80         # call

  #input
  mov $3, %eax      # 3 syscall read
  mov $0, %ebx      # 0 filedescriptor stdin
  mov $(name), %ecx # address of name
  mov $6, %edx      # max length
  int $0x80

  #check input length
  mov $5, %ebx             #######################
  cmp %ebx, %eax           # <-- HERE or         #
  jg problem #to long      # <-- HERE            #
                           # IS SOMETHING WRONG  #
                           # I GUESS !!          #
  #output of $name         #######################
  mov $1, %ebx
  mov $name, %ecx
  mov %eax, %edx
  mov $4, %eax
  int $0x80

  jmp exit

problem:
  #output of $err
  mov $4, %eax
  mov $1, %ebx
  mov $err, %ecx
  mov 10, %edx
  int $0x80

exit:
  mov $1, %eax      # 1 syscall exit
  mov $0, %ebx      # returnvalue
  int $0x80
Était-ce utile?

La solution 2

Jester Answered:

You forgot to reserve space for the name. Try: name: .space 6. The fault is caused by a missing $ sign on the line mov 10, %edx. Learn to use a debugger.

Thanks to him! Now it works.

I changed the line name: .ascii "" to name: .space 6
and added a $ to mov 10, %ebx make it look like mov $10, %ebx!

Autres conseils

I really like the way you have done your homework in advance, and shown us some written code.

I super especially like the fact that you commented your code; excellent sir. Now I'm happy to help (if this is indeed helpful).

Okay, I'll focus on this stuff...

mov $5, %ebx             #######################
cmp %ebx, %eax           # <-- HERE or         #
jg problem #to long      # <-- HERE            #
                         # IS SOMETHING WRONG  #
                         # I GUESS !!          #
#output of $name         #######################

Worry not; nothing's really wrong. Everybody goes through this (including me).

Wow, where do we start.

Okay, I got it, a 15 minute exercise that will clear this matter up for you.

First, question: Are you familiar with signed as opposed to unsigned math at the register and bit level ? If not, prepare for some brain cell activity.

Secondly, to confuse things even further, assembler nomenclature and syntax will have an effect on what result you can expect (and what you wind up getting).

I'm really not savvy with the GNU assembler, so let me explain how I got a grasp on this.

The instruction Cmp This,That can be thought to mean something like...

  • What is the relationship of This with respect to That ?

More to the point, is This above, equal, or below That ?

I will stick with unsigned integer for now; because it's simplest.

Not sure about GNU, but with other assemblers, when you are using unsigned thinking in your brain, you want to use the instructions JA and JB and JE for comparisons.

JA is "Jump if above" JB is "Jump if below" JE is "Jump if equal"

The JG that you are using could be signed arithmetic; we'll worry about that later.

To confuse things even more, assembler syntax can reverse the order of the operands, and you will need to alter your mental state and think this question instead of the original one...

  • What is the relationship of That with respect to This ?

Okay, now, setting breakpoints, here's how you get your mind into this..

 Mov $5, %ebx               # Your original way
 Cmp %ebx, %eax             # Same compare
 Ja problem                 # Unsigned maybe jump


 Mov $5, %ebx               # Same steps
 Cmp %ebx, %eax             # Same steps
 Jb problem                 # Opposite jump sense


 Mov $5, %ebx               # Now, similar steps
 Cmp %eax, %ebx,            # Comparing them in an opposite manner
 Ja problem                 # Watch to see if this jump hits


 Mov $5, %ebx               # Arrange them again
 Cmp %eax, %ebx,            # Backwards compare, but,,,,
 Jb problem                 # Opposite opinion of the flags

These four examples assume that the two numbers (in Ebx and Eax) are different. If they turn out to be the same, then we will have an extended discussion on this matter another day.

This is, again, all about unsigned integer arithmetic. For some, that would be elementary stuff; for others, worry not, struggling around with confusion is, honestly, the norm. I know I did it.

Anyway, set breakpoints and run each of those three line snippets, and get the idea in your head, "...What is the relationship of THIS with respect to THAT ?..."

As you step through each 3 line snippet, you'll get the idea.

If you screw up, it will make sense the second (or third or fourth or fifth) time.

Not sure if this is helping or not, just thought I would type it up for others who are new to the regs and bits and flags and stuff.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top