If Else in Assembly Language

Assembly language doesn’t have high-level constructs like if/else statements, so we’ll implement the logic using conditional jumps. This example uses x86 assembly syntax.

section .data
    msg1 db '7 is even', 0
    msg2 db '7 is odd', 0
    msg3 db '8 is divisible by 4', 0
    msg4 db 'either 8 or 7 are even', 0
    msg5 db ' is negative', 0
    msg6 db ' has 1 digit', 0
    msg7 db ' has multiple digits', 0

section .text
    global _start

_start:
    ; Example 1: if 7%2 == 0
    mov eax, 7
    mov ebx, 2
    xor edx, edx
    div ebx
    cmp edx, 0
    je .even
    mov ecx, msg2
    jmp .print1
.even:
    mov ecx, msg1
.print1:
    call print_string

    ; Example 2: if 8%4 == 0
    mov eax, 8
    mov ebx, 4
    xor edx, edx
    div ebx
    cmp edx, 0
    jne .skip
    mov ecx, msg3
    call print_string
.skip:

    ; Example 3: if 8%2 == 0 || 7%2 == 0
    mov eax, 8
    mov ebx, 2
    xor edx, edx
    div ebx
    cmp edx, 0
    je .print_either
    mov eax, 7
    xor edx, edx
    div ebx
    cmp edx, 0
    jne .skip2
.print_either:
    mov ecx, msg4
    call print_string
.skip2:

    ; Example 4: if-else-if
    mov eax, 9  ; num := 9
    cmp eax, 0
    jl .negative
    cmp eax, 10
    jl .one_digit
    mov ecx, msg7
    jmp .print_num
.negative:
    mov ecx, msg5
    jmp .print_num
.one_digit:
    mov ecx, msg6
.print_num:
    call print_string

    ; Exit the program
    mov eax, 1
    xor ebx, ebx
    int 0x80

print_string:
    ; Function to print a null-terminated string
    ; Address of string should be in ecx
    push ecx
    mov edx, 0
.count:
    inc edx
    inc ecx
    cmp byte [ecx], 0
    jne .count
    pop ecx
    mov eax, 4
    mov ebx, 1
    int 0x80
    ret

This assembly code implements the logic of the original example using conditional jumps and comparisons. Here’s a breakdown of the translation:

  1. We define our messages in the .data section.

  2. The main logic is in the .text section, starting from the _start label.

  3. For each if statement, we perform the necessary calculations and use cmp (compare) and conditional jump instructions (je, jne, jl) to implement the branching logic.

  4. We use a print_string function to output messages, which is a simplification of the fmt.Println function in the original code.

  5. The if num := 9; statement is translated by simply moving the value 9 into the eax register before the comparisons.

Note that assembly language is much more verbose and requires explicit management of registers and memory. The logical structure of the original code is preserved, but the implementation details are quite different due to the low-level nature of assembly.

To assemble and run this code (assuming you’re using NASM on a Linux system):

$ nasm -f elf if_else.asm
$ ld -m elf_i386 -o if_else if_else.o
$ ./if_else

This will output the results of our conditionals, similar to the original example.

Remember that assembly language doesn’t have built-in high-level constructs, so we have to implement them manually using the available instructions and control flow mechanisms.