A
A
Alexander Bondarenko2020-11-10 23:35:52
C++ / C#
Alexander Bondarenko, 2020-11-10 23:35:52

Why are all labels executed?

Hello, why do all the labels work for me and the ans variable contains the result of all actions and not just a specific one, thanks in advance!

int v, ans, aR1,aR2,aR3;
_asm 
{

    mov eax, 1
    mov ebx, 10

    cmp eax, 1
    jz m1
    cmp eax, 2
    jz m2
    cmp eax, 3
    jz m3
    cmp eax, 4
    jz m4
    jmp m5
    m1:
        imul ebx, 1000
        mov ans, ebx
    m2:
        mov ans, ebx
    m3:
        imul ebx, 10
        mov ans, ebx
    m4:
        imul ebx, 100
        mov ans, ebx
    m5:
        xor eax, eax

}
cout << "SWITCH";
cout << ans;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Adamos, 2020-11-11
@bond_1013

It's not a high-level language with case, it's asm.
Here labels are just labels, they just tell the assembler the address of the next line to put it in the binary code of the jmp command, without adding any exit logic to the next label.
You need to add m6: at the end and jmp m6 after each option except the last one.

R
Rsa97, 2020-11-10
@Rsa97

Why shouldn't they "work"? What can prevent this?
Just imagine that 1 is written in AX and go through the entire program in your mind step by step.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question