Take the 2-minute tour ×

I have this code

        add cx, bx
        cmp cx, 0FFFFh
        JBE convert

I'm trying to add cx and bx registers, each have the same value of FFFF, instead of getting 1FFFE, I get only FFFE, and when I try to use JBE to jump to convert loop, JBE istruction does nothing because ecx register now contain only FFFE, but not 1FFF, so how can i fix this code to make ecx contain 1FFFE and how do I compare to check if it is still 16 bits or not. I cannot use any 32 bits registers which make thing more complicated

Thank in advance

share|improve this question
The maximum value a 16 bit register can contain is FFFF.. –  Blorgbeard May 20 '13 at 1:40
Why can't you use 32-bit registers? (This would be a lot easier if you used ecx.) –  michaelb958 May 20 '13 at 1:43
It may possibly help if you check OF (overflow flag) in FLAGS after the add instruction. –  Jonathon Reinhart May 20 '13 at 1:45
overflow would not work since i can only use 16 bits registers, it will never get over flow –  bluebk May 20 '13 at 3:42
add comment

1 Answer

up vote 1 down vote accepted

    clc            ;clear carry flag

    add cx,bx

    jnc convert    ;jump no carry

    cmp cx,FFFEh   ;This now needs to be true, only FFFF+FFFF will succeed

                    because it generates a carry AND matches the cmp

    Jnz convert    ;will let it through

This will work, but it's very limited and simplistic

To "count" the carry use adc dx,0 inserted after jnc convert

The carry flag is set if a register goes round the clock, so it acts like a single bit

ADD FFFF+2 will set it off, giving you a +ve carry flag and 0001 in the register

You can store that flag count in a separate register with adc [other register],0

using the carry flag allows you slap your registers together to count up to something like

1,208,741,363,432,547,555,475,424 with 4x16 bit registers

which is quite a lot and miles better than 65,534

share|improve this answer
then if i want to compare any sum that is larger than the value of 16 bits how should i do that? –  bluebk May 20 '13 at 3:38
The first part does that, jnc –  ady May 20 '13 at 9:00
If you want to compare two separate numbers both larger than 16 bits in a 16 bit environment then you are going to need to learn how to use multiple registers –  ady May 20 '13 at 9:29
add comment

Your Answer


