Very weird problem about AMD64 ASM in Linux

SuperMMX supermmx at gmail.com
Thu May 25 07:21:55 CEST 2006


hi, experts,

I met a very weird problem when porting some ASM codes to AMD64 Linux,
the sample sources are enclosed below:

(Please CC me when replying, i am not subscribed )

=============== ASM codes tt.asm ================
GLOBAL OKOK
GLOBAL _OKOK

OKOK:
_OKOK:  
        cmp  rdi, byte 0       ; nDirection
        jge  short SUB_L
ADD_L:  
        mov rax, 1
        ret
SUB_L:  
        je short DONE
        mov rax, -1
        ret
DONE:   
        mov rax, 0
        ret
================ ASM end ================

================ C++ codes ss.cpp ==============
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
 
extern "C" {
    int OKOK(int nDirection);
}
 
int main(int argc, char*argv[]) {
    int result =  OKOK(-8);
    printf("result -1 = %d\n", result);
 
    result =  OKOK(8);
    printf("result 1 = %d\n", result);
 
    result =  OKOK(0);
    printf("result 0 = %d\n", result);
 
    return 0;
}
================ CPP end =============

yasm -f elf -m amd64 -o tt.o tt.asm
g++ -o ss ss.cpp tt.o
./ss
result -1 = -1
result 1 = -1
result 0 = 0

what OKOK function does is very simple, 
if (para > 0)
  return -1;
else if (para < 0) 
  return 1;
else
  return 0;

but in my AMD64 box, it never returns "1".. :(
that is , it always jumps (JGE) after the first cmp, 
and i get the same results from other AMD64 users.

And the GDB debug result is :
============= GDB result ==============
Breakpoint 1, 0x0000000000400c40 in _OKOK ()
(gdb) disassemble
Dump of assembler code for function _OKOK:
0x0000000000400c40 <_OKOK+0>:   cmp    $0x0,%rdi
0x0000000000400c44 <_OKOK+4>:   jge    0x400c4e <_OKOK+14>
0x0000000000400c46 <_OKOK+6>:   mov    $0x1,%rax
0x0000000000400c4d <_OKOK+13>:  retq   
0x0000000000400c4e <_OKOK+14>:  je     0x400c58 <_OKOK+24>
0x0000000000400c50 <_OKOK+16>:  mov    $0xffffffffffffffff,%rax
0x0000000000400c57 <_OKOK+23>:  retq   
0x0000000000400c58 <_OKOK+24>:  mov    $0x0,%rax
0x0000000000400c5f <_OKOK+31>:  retq   
End of assembler dump.
(gdb) info registers rdi eflags
rdi            0xfffffff8       4294967288
eflags         0x202    514
(gdb) si
0x0000000000400c44 in _OKOK ()
(gdb) info registers rdi eflags
rdi            0xfffffff8       4294967288
eflags         0x302    770
(gdb) si
0x0000000000400c4e in _OKOK ()
============== GDB result end =============

Can you see the problem ? I am so confused.
Thank you

Best regards,
SuperMMX
-- 
A. Because it makes the logic of the discussion difficult to follow.
Q. Why shoudn't I top post?
A. No.
Q Should I top post?

A: Because it destroys the flow of the conversation
Q: Why is it bad?
A: No, it's bad.
Q: Should I top post in replies to mailing lists? 



More information about the discuss mailing list