Author Topic: what's wrong with my code  (Read 34251 times)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: what's wrong with my code
« Reply #15 on: March 12, 2013, 08:23:21 PM »
Quickly... for a 64-bit system, we need to tell ld we want 32-bit code...
Code: [Select]
ld ex.o -o ex -melf_i386

More later,
Frank


Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: what's wrong with my code
« Reply #16 on: March 13, 2013, 12:25:52 AM »
can I do this?

mov ecx, 40
mov eax, 4
mov ebx, 1

I don't quite understand why we can't use ecx?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: what's wrong with my code
« Reply #17 on: March 13, 2013, 10:47:15 AM »
Sure you can use ecx. If this is supposed to be the setup for a sys_write, 40 isn't going to be valid memory to find the buffer...

In my "print the matrix" routine, I used ecx as a pointer into the buffer as I filled it up with digits/characters so it was supposed to be "all set" when I got to the sys_write, but I screwed up! I decremented ecx to the beginning of my buffer, but didn't put a space in the last [ecx]. A classic "fencepost" or "off-by-one" error. My bad! It kinda works okay anyway, but it definitely isn't right. I'll fix it if I'm in the mood...

The "mov cx, 5" was left over from your code. Doesn't do any good, but it doesn't do any harm. I tried to leave your code alone (consistent with making it work with dwords) and just add the "print the matrix" routine so you could track down what was happening more easily... and I didn't even get that right... sigh...

Best,
Frank


Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: what's wrong with my code
« Reply #18 on: March 13, 2013, 04:06:38 PM »
Code: [Select]
digits db "aa", 0xA, 0XD
length equ $ -digits

mov eax, 4
mov ebx, 1
mov ecx, 3
mov edx, length
 

What is the problem with this code?


Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: what's wrong with my code
« Reply #19 on: March 13, 2013, 08:01:57 PM »
Ok, I tried using printf, but I get segmentation fault errors. Why is that?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: what's wrong with my code
« Reply #20 on: March 13, 2013, 08:10:09 PM »
Well, your first code looks like it's going to execute the data. My crystal ball tells me you're probably mutilating the stack, trying to use "printf"...

Best,
Frank


Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: what's wrong with my code
« Reply #21 on: March 13, 2013, 09:27:01 PM »
Thanks.

I don't really understand how the stack works... Do i have to clean it at the end of each label? When do I have to clean it?

Someone said I got fragmentation fault because I was running a 32 bit code in a 64 bit machine. Is he right?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: what's wrong with my code
« Reply #22 on: March 13, 2013, 11:37:43 PM »
Probably. I haven't got a 64-bit nachine, so I don't know what it'll do. 32-bit code should run fine on a 64-bit machine. Source code suitable for 32-bit code, assembled to a 32-bit linkable file, and linked to some mysterious hybrid of 32- and 64-bit code is probably "undefined"... but it doesn't seem to be working out well!

You and I have both posted examples of "cleaning up the  stack" to the "printing in asm" topic. Probably better to discuss it there...

Best,
Frank


Offline maplesirop

  • Jr. Member
  • *
  • Posts: 60
Re: what's wrong with my code
« Reply #23 on: March 14, 2013, 08:55:48 PM »
can we compare

[matrix + edi + esi]

and

[matrix + edi + esi + 4]

it seems that it doesn't work, but i am not sure and when is the stack cleared?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: what's wrong with my code
« Reply #24 on: March 15, 2013, 04:42:39 AM »
Ummm... you mean like?
Code: [Select]
cmp [matrix + edi + esi], [matrix + edi + esi + 4]
? No, we can't. Only one memory reference at a time, generally (we only have one address bus). We'd have to do:
Code: [Select]
mov eax, [matrix + edi + esi]
cmp [matrix + edi + esi + 4], eax

I'm not sure what you mean about the stack...
Code: [Select]
push eax
push ebx
push ecx
push format_string
call printf
add esp, 16 ; <- "remove" 4 parameters from stack

Like that?

Best,
Frank