Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Bugs and Fixes

Case statement

A community discussion post from the original code:zIRON forum.

0CodErr member
Posts
201
Threads
37
Location
Unknown

Case statement

program RAW_IMAGE 'test';

#set bits 32;

case (esp) {
state eax: edx = 1; break;
state ebx: edx = 2; break;
state ecx: edx = 3; break;
state edx: edx = 4; break;
state esi: edx = 5; break;
state edi: edx = 6; break;
state ebp: edx = 7; break;
default:   edx = 8; 
}

Result:

Seems here is unnecessary first jump.

00000000  EB00              jmp short 0x2
00000002  3BE0              cmp esp,eax
00000004  750A              jnz 0x10
00000006  BA01000000        mov edx,0x1
0000000B  E947000000        jmp dword 0x57
00000010  3BE3              cmp esp,ebx
00000012  7507              jnz 0x1b
00000014  BA02000000        mov edx,0x2
00000019  EB3C              jmp short 0x57
0000001B  3BE1              cmp esp,ecx
0000001D  7507              jnz 0x26
0000001F  BA03000000        mov edx,0x3
00000024  EB31              jmp short 0x57
00000026  3BE2              cmp esp,edx
00000028  7507              jnz 0x31
0000002A  BA04000000        mov edx,0x4
0000002F  EB26              jmp short 0x57
00000031  3BE6              cmp esp,esi
00000033  7507              jnz 0x3c
00000035  BA05000000        mov edx,0x5
0000003A  EB1B              jmp short 0x57
0000003C  3BE7              cmp esp,edi
0000003E  7507              jnz 0x47
00000040  BA06000000        mov edx,0x6
00000045  EB10              jmp short 0x57
00000047  3BE5              cmp esp,ebp
00000049  7507              jnz 0x52
0000004B  BA07000000        mov edx,0x7
00000050  EB05              jmp short 0x57
00000052  BA08000000        mov edx,0x8

Next (true instead of esp) give error 'Instruction usage is not supported!'

program RAW_IMAGE 'test';

#set bits 32;

case (true) {
state eax: edx = 1; break;
state ebx: edx = 2; break;
state ecx: edx = 3; break;
state edx: edx = 4; break;
state esi: edx = 5; break;
state edi: edx = 6; break;
state ebp: edx = 7; break;
default:   edx = 8; 
}

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Thanks, fixed both of these for next release.

0CodErr member
Posts
201
Threads
37
Location
Unknown

In the listing above there is

0000000B  E947000000        jmp dword 0x57

It can be coded as short variant.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

This is already fixed. Thanks :)

0CodErr member
Posts
201
Threads
37
Location
Unknown

I would like to discuss about 'case' statement. What it will be when it is ready?

Maybe it would be more convenient to be able to test the range:

case (gp_ax) {
state -7..3: break;
state 5..100: break;
state 0x80..0x200: break;
state gp_dx..gp_cx: break;
state gp_si..0xffff: break;
state 0x12345678..gp_di: break;
default:   
}

Or maybe this will be useful:

case (gp_ax) {
state 0, 2, 5, 100: break;
state gp_bx, gp_cx: break;
state gp_dx, 0x20, 0b111: break;
default:   
}

Or even so:

case (gp_ax, gp_dx, gp_cx) {
state 0,1,2,3: break;
state 4,6,8: break;
state gp_si..gp_di: break;
default:   
}

What do you think about it?

And what you are really planned with 'case' statement?

Colin admin
Posts
928
Threads
49
Location
Bulgaria

you can place multiple states in front of one another (but maybe i will add using comma for additional states)

// example
case (gp_ax) {
  state 0:
  state 5:
  state 21:
    eax = 512;
    break;

  state 10:
  state 61:
    eax = 1024;
    break;
}

Ranges may be a good idea, but I will first concentrate on fixing any other bugs before extending any high level features.

0CodErr member
Posts
201
Threads
37
Location
Unknown

What do you think about this variant:

case (gp_ax) {
state  < gp_bx: break;
state <= gp_cx: break;
state == gp_dx: break;
state <> gp_si: break;
state => gp_di: break;
state  > gp_bp: break;
default:   
}

Also it can be extended by this way:

case (gp_ax) {
state  < gp_bx, <= gp_cx: break;
state == gp_dx, <> gp_si: break;
state => gp_di,  > gp_bp: break;
default:   
}
Colin admin
Posts
928
Threads
49
Location
Bulgaria

I have added this to my list and will consider this implementation.