0

Overall, I am trying to compare the value of the first element of an array passed into my assembly function from a c++ program with a constant value and preform a jump if they are equal.

I can view the value of %rdi in gdb with x /c $rdi and it outputs 0x7fffffffde80: 97 'a' I would expect that cmp $97, %rdi and then preforming a jump if they are equal would work, but alas the comparison fails as if these two values are not equal.

I've built a small test program demonstrating what I am attempting to do. The program should get stuck in an infinite loop if the first element of the array being passed in is not an 'a', otherwise it should exit normally. when ran though, I get stuck in an infinite loop regardless of the values passed in the array.

Any insight onto what is going on here would be very much appreciated.

In my driver cpp:

#include <iostream>
#include <stdio.h>

extern int _asmTest(char c[]) __asm__("_asmTest");


int main() 
{
    char arr[2] = {'a','b'};  
    int test = _asmTest(arr);
    std::cout << "First element was 'a'" << std::endl;
    return 0;
}

In my asm:

.global _asmTest
.text

_asmTest:
    push   %rbp
    mov    %rsp, %rbp

    # Go to infinite loop if arg1's first element is not 'a'
    loop:
    cmp    $97, %rdi
    je     continue
    jmp    loop   

    continue:
    mov    %rbp, %rsp
    pop    %rbp
    ret
MDausch
  • 36
  • 6
  • 2
    `c` is an array, that is a pointer. You want to dereference it. Try `cmpb $97, (%rdi)` instead. Note that the `x` command in `gdb` is specifically for examining memory. If you used `p/a $rdi` you would have seen the actual value in `rdi`, which is `0x7fffffffde80` (also printed by `x` as the address). – Jester Jul 09 '19 at 18:57
  • @jester, I've tried that as well thinking the same thing, but there was no difference. It still gets stuck in the loop – MDausch Jul 09 '19 at 18:59
  • 1
    You did something wrong, e.g. not running the version you compiled from the updated source or something similar. Works here as expected. – Jester Jul 09 '19 at 19:01
  • 1
    @jester Ahh I see, I wasn't doing cmpb Thank you for the help! If you want to put it as an answer, I can mark it as accepted. – MDausch Jul 09 '19 at 19:19

0 Answers0