Help needed Collatz Conjecture x86

Hello ! I am facing an issue for the the test ‘test_negative_value_is_an_error’ for an assembly code x86_64

I have a label named .invalid which handles all invalid input :
'test_zero_is_an_error’ & ‘test_negative_value_is_an_error’

My code works for both and I am able to pass the ‘test_zero_is_an_error’ with -1, it does not work with ‘test_negative_value_is_an_error’ which send me this error log :
Expected -1 Was 389 I thought it was due to unsigned int or something but I have no clue how to fix it lol I want to kms

Here are my input verification in the beginning of my code :

   test rdi, rdi
    js .invalid 

    cmp rdi, 0
    je .invalid

I’ve tried several implemantion for the .invalid label :

.invalid:
    xor rax, rax
    sub rax, 1
    ret
.invalid 
   xor rax, rax
   mov rax, -1
   ret

Both implementation works for the ‘test_zero_is_an_error’ but not for ‘test_negative_value_is_an_error’

Can someone help me lol :33 ?

Got curious and tried the exercise here. The problem seems to be that the input is an int (which in this case means 32bits) and you’re using 64-bit registries. I believe the last 4 bytes had garbage value in this case.

BTW, CMP also sets the sign flag, so you don’t need TEST here.

A seemingly good reference

You were right ! I managed to complete all the test after changing to 32 bits.
Sorry for the inconvenience - I am a noob lol.

Thanks for the ressources and the help - Good to close the ticket

1 Like

I’m just a newbie as well. I’m still finishing this book on the subject, but it’s been almost an year already.