Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with Casear cipher in x86 assembly

I have the following instructions as part of a Casear cipher program.

overFlow:
    sub bl, 1Ah
    ret

underFlow:
    add bl, 1Ah
    ret

correctFlow:
    cmp bl, 7Ah
    jg overFlow
    cmp bl, 61h
    jl underFlow
    ret

enc_byte:
    add bl, encOffset
    call correctFlow
    ret

An ASCII lowercase letter is put into BL and after enc_byte is called, it shifts the letter by encOffset letters and corrects for an overflow.

But for some reason the compare in correctFlow doesn’t work correctly. When BL=8Dh in correctFlow, the jg overFlow instruction does not jump, and instead jl underFlow jumps after the second cmp. Why is this happening? 8Dh is clearly greater than 7Ah, so why doesn’t it jump as expected?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know the returns are weird. The overFlow and underFlow labels are the ones that return the call to correctFlow. This is intentional and as far as I know, doesn’t have anything to do with the issue.

>Solution :

This happens because jg and jl treat the outcome of cmp as if the two operands were signed numbers.

7Ah and 8Dh represent signed numbers +122 and -115, respectively. Obviously, the latter is the smallest.

What you need is unsigned comparison. Use instructions ja and jb instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading