Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

IF...ELSE statements in assembly

singerksingerk Posts: 11Member
Hello!

How do you begin to make a procedure for if..else statements in assembly that would work in most applications?

If this has been done before, lead me to past messages.
If not, I would like to see some ideas.

OK, thank you in advance for your help.

Comments

  • mfroebmfroeb Posts: 53Member
    in high level language (basic):
    IF variable=0 THEN
    instructions
    ELSE
    instructions
    END IF

    in assembler:

    mov eax,variable ;move variable into cpu register
    cmp eax ;compare (with 0)
    jnz else_label ;if not zero, then execute else instructions
    if_label: ; this label doesn't serve any practical pupose,
    ; just for clarity
    instructions ;execute if instrcutions
    else_label:
    instructions
    endif_label: ; this label also serves no practical purpose


    : Hello!
    :
    : How do you begin to make a procedure for if..else statements in assembly that would work in most applications?
    :
    : If this has been done before, lead me to past messages.
    : If not, I would like to see some ideas.
    :
    : OK, thank you in advance for your help.
    :
    :

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]Better way:[/blue][code]
    cmp operand1, operand2
    j_xxx @label_on_true
    cmp operand1, operand2
    j_xxx @label_on_true
    cmp operand1, operand2
    j_xxx @label_on_true
    ...
    [/code][blue]This is useful when you want to make a section of code like [b]switch()[/b] statement in "C". You do your jumps when condition is true and fall on the next opcode when it is false. Your J_xxx opcode will be faster if it falls to the next statement and slower if you actually jump. The next code will be slower:[/blue][code]
    cmp op1, op2
    j_false @check_next_IF

    @True1:
    ; ...
    jmp @switch_end

    @check_next_IF:
    cmp op1, op2
    j_false @check_next_IF

    @True2:
    ; ...
    jmp @switch_end

    ; --------- etc.
    [/code]
  • mfroebmfroeb Posts: 53Member
    you're right. but i don't do switch statements in assembly. i don't even do if..then..else statements in assembly. i only code loops in assembly because my c++ compiler cannot compile mmx/3dnow/3dnow+/sse/sse2 (for games or benchmarks).

    btw: don't forget to visit [blue]aac.net.tf[/blue] for the best audio cd ripper for windows!

    : [blue]Better way:[/blue][code]
    : cmp operand1, operand2
    : j_xxx @label_on_true
    : cmp operand1, operand2
    : j_xxx @label_on_true
    : cmp operand1, operand2
    : j_xxx @label_on_true
    : ...
    : [/code][blue]This is useful when you want to make a section of code like [b]switch()[/b] statement in "C". You do your jumps when condition is true and fall on the next opcode when it is false. Your J_xxx opcode will be faster if it falls to the next statement and slower if you actually jump. The next code will be slower:[/blue][code]
    : cmp op1, op2
    : j_false @check_next_IF
    :
    : @True1:
    : ; ...
    : jmp @switch_end
    :
    : @check_next_IF:
    : cmp op1, op2
    : j_false @check_next_IF
    :
    : @True2:
    : ; ...
    : jmp @switch_end
    :
    : ; --------- etc.
    : [/code]
    :

  • blipblip Posts: 756Member
    In many cases you don't even need to do a CMP list or anything similar. I prefer jump/call tables if the list gets too big, here's how I usually set it up:
    [code]
    Invalid:
    ;Do something for input, maybe get a key press.
    cmp eax,LowBound
    jbe Invalid
    cmp eax,HighBound
    jae Invalid
    ;I usually do a SUB here, mostly for converting ASCII to hex.
    jmp [JumpTable+eax*2]
    ;...
    JumpTable Label0, Label1, Label2, Label3 ;etc...
    [/code]

    Remember that the IVT is also a big FAR CALL table, just that the INT instruction implicitly does a PUSHF before the CALL.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]Yep.
    It is a good technique when your cases are from the linear sequence, like:

    case 1:
    case 2:
    case 3:
    ; etc.

    If the cases are not from the linear sequence, like:

    case 1289:
    case 34:
    case 12:
    case 1:

    then you need a huge table to cover it. I usually have a routine which calls/jumps to the handler:[/blue][code]
    .DATA
    arrHandlerMap DD case1,foo1,case2,foo2,case3,foo3,0,0

    .CODE
    foo1 proc
    ret
    foo1 endp

    foo2 proc
    ret
    foo2 endp

    foo3 proc
    ret
    foo3 endp

    ; -----------------------------------------------------
    ; INPUT: ESI <- map address (first DWORD is a case value,
    ; second is an handling function address)
    ; EAX <- value to make a switch on
    ; -----------------------------------------------------
    switch proc
    xor edx, edx ; I need zero register to test the array's end

    CHECK_THE_CASE:
    cmp eax, [esi]
    je CALL_IT

    ; --- move to next:
    add esi, 8 ; Skip 2 DWORDs
    test [esi+4], edx ; See if array ends here
    jnz CHECK_THE_CASE ; Not yet

    ; Array ended. Call some def. section or return err. val.
    ret

    CALL_IT:
    call [esi+4]
    ret
    switch endp
    [/code]
  • mfroebmfroeb Posts: 53Member
    : In many cases you don't even need to do a CMP list or anything similar. I prefer jump/call tables if the list gets too big, here's how I usually set it up:
    : [code]
    : Invalid:
    : ;Do something for input, maybe get a key press.
    : cmp eax,LowBound
    : jbe Invalid
    : cmp eax,HighBound
    : jae Invalid
    : ;I usually do a SUB here, mostly for converting ASCII to hex.
    : jmp [JumpTable+eax*2]
    : ;...
    : JumpTable Label0, Label1, Label2, Label3 ;etc...
    : [/code]
    :
    : Remember that the IVT is also a big FAR CALL table, just that the INT instruction implicitly does a PUSHF before the CALL.
    :

    If you really need a jump table or if you write complex programs in assembly, then this is good, indeed.
    I only have small functions (about 100-250 lines assembly), so there is no need for jump tables for me.
  • korkor Posts: 198Member
    : in high level language (basic):
    : IF variable=0 THEN
    : instructions
    : ELSE
    : instructions
    : END IF
    :
    : in assembler:
    :
    : mov eax,variable ;move variable into cpu register
    : cmp eax ;compare (with 0)
    : jnz else_label ;if not zero, then execute else instructions
    : if_label: ; this label doesn't serve any practical pupose,
    : ; just for clarity
    : instructions ;execute if instrcutions
    : else_label:
    : instructions
    : endif_label: ; this label also serves no practical purpose
    :
    :
    : : Hello!
    : :
    : : How do you begin to make a procedure for if..else statements in assembly that would work in most applications?
    : :
    : : If this has been done before, lead me to past messages.
    : : If not, I would like to see some ideas.
    : :
    : : OK, thank you in advance for your help.
    : :
    : :
    :
    :

    Did anyone else notice that a jmp to endif_label is needed before the else instructions, so they aren't executed?

  • blipblip Posts: 756Member
    : Did anyone else notice that a jmp to endif_label is needed before the else instructions, so they aren't executed?

    I noticed, but on some occasions you actually do want to execute the following instructions (not too likely, though).
  • singerksingerk Posts: 11Member
    WOW!

    Thank you for giving me ideas! I never expected such a great number of responses.

    Thank you code gurus and your clever minds.

    I am starting to get the hang of this assembly thing. I have so many concepts and information that I still need to get straight to fully understand this stuff,and will post more easy to solve questions.

    THANK YOU ALL!!!
  • mfroebmfroeb Posts: 53Member
    : : Did anyone else notice that a jmp to endif_label is needed before the else instructions, so they aren't executed?
    :
    : I noticed, but on some occasions you actually do want to execute the following instructions (not too likely, though).
    :

    the jmp before else instructions is right. i just forgot it, because i have never written any if..then..else constructs in assembly yet.
Sign In or Register to comment.