views:

111

answers:

1

Hi folks, I'm trying to do something which I figure would be rather easy, though I'm beign proven terribly wrong. I'm probably doign something really dumb. Before I explain, here's my code:

.data
mynum:
    .int 75

format:
    .string "Value: %d\n"

.text
.globl main
main:

    movl    $0, %eax

    # Add the number
    addl    $mynum, %eax

    # Print it out
    pushl   %eax
    pushl   $format
    call    printf
    addl    $8, %esp


    movl    $1,%eax
    xorl    %ebx,%ebx
    int $0x80

    ret

Pretty straightforward:

  1. Add a number onto the accumulator (that has been declared in the .data section)
  2. Print the number out

Now the problem that I'm having is this: when the code runs, instead of printing Value: 75 like I'd hope, it's giving me Value: 134518172.

My assumption is that a bunch of bits are being passed that should be being passed. Where they're coming from, I'm not sure. In fact, I don't even know what they are.

One thing I've tried is using movb to clear out the first and last halves of the accumulator, but it hasn't made any significant difference (it has changed the number, but only by ~1000 in either direction):

movb  $0, %ah

and

movb  $0, %al

Thanks in advance

+2  A: 
 addl    $mynum, %eax

mynum is an address. So you're getting a pointer value in your register. You need to dereference it.

By the way, this should be obvious if you're debugging it line by line. You should really try one out.

Bahbar
off the top of my head: `addl [$mynum,] %eax` - isn't that the way to de-reference?
dboarman
Bahbar
Fortunately this isn't an Assembly class...it's Comp Arc. The prof hasn't really explained much more than "the six CPU registers". You can tell me how to dereference it all you want :D.
mattbasta
Just wanted to add what my final solution was: `addl (mynum), %eax`Thanks for your help, guys
mattbasta