だんぷにっき3日目(IF文)

制御構造編

ともじさんの「初心者のためのポイント学習C言語」より
http://www9.plala.or.jp/sgwr-t/c/S/smpl06-1.html
を一部改変(浮動小数点を使うと、アセンブラの命令が浮動少数点を使ったものになってしま
うので、
整数の変数を使うことに)

まずは、if〜elseを

#include

int main(void)
{

int x,y,z;

printf("整数 x を入力してください");
scanf("%d",&x);
printf("整数 y を入力してください");
scanf("%d",&y);

if(x>=y){
  z=x-y;
  printf("x - y -%f\n",z);
}
else{
  z=y-x;
  printf("y - x -%f\n",z);
}


return 0;
}


0:000> u main main+100
hello2!main:
00401010 55 push ebp
00401011 8bec mov ebp,esp
00401013 83ec0c sub esp,0Ch
00401016 6800404200 push offset hello2!__rtc_tzz (hello2+0x24000)(00424000)
0040101b e885010000 call hello2!printf (004011a5)
00401020 83c404 add esp,4
00401023 8d45f8 lea eax,[ebp-8]
00401026 50 push eax
00401027 681c404200 push offset hello2!__rtc_tzz (hello2+0x2401c)(0042401c)
0040102c e80c010000 call hello2!scanf (0040113d)
00401031 83c408 add esp,8
00401034 6820404200 push offset hello2!__rtc_tzz (hello2+0x24020)(00424020)
00401039 e867010000 call hello2!printf (004011a5)
0040103e 83c404 add esp,4
00401041 8d4dfc lea ecx,[ebp-4]
00401044 51 push ecx
00401045 683c404200 push offset hello2!__rtc_tzz (hello2+0x2403c)(0042403c)
0040104a e8ee000000 call hello2!scanf (0040113d)
0040104f 83c408 add esp,8
00401052 8b55f8 mov edx,dword ptr [ebp-8]
00401055 3b55fc cmp edx,dword ptr [ebp-4]
00401058 7c1c jl hello2!main+0x66(00401076)//if(x>=y) |
0040105a 8b45f8 mov eax,dword ptr [ebp-8]
0040105d 2b45fc sub eax,dword ptr [ebp-4]
00401060 8945f4 mov dword ptr [ebp-0Ch],eax
00401063 8b4df4 mov ecx,dword ptr [ebp-0Ch]
00401066 51 push ecx
00401067 6840404200 push offset hello2!__rtc_tzz (hello2+0x24040)(00424040)
0040106c e834010000 call hello2!printf (004011a5)
00401071 83c408 add esp,8
00401074 eb1a jmp hello2!main+0x80 (00401090)
00401076 8b55fc mov edx,dword ptr [ebp-4]
00401079 2b55f8 sub edx,dword ptr [ebp-8]
0040107c 8955f4 mov dword ptr [ebp-0Ch],edx
0040107f 8b45f4 mov eax,dword ptr [ebp-0Ch]
00401082 50 push eax
00401083 684c404200 push offset hello2!__rtc_tzz (hello2+0x2404c)(0042404c)
00401088 e818010000 call hello2!printf (004011a5)
0040108d 83c408 add esp,8
00401090 33c0 xor eax,eax
00401092 8be5 mov esp,ebp
00401094 5d pop ebp
00401095 c3 ret



jl ・・・・(op1 < op2)





あとは、色々パターンを変えてみましょう。

パターン1
if(x==y){
処理A
}
else{
処理B
}

cmp x,y
jne (処理Bのアドレス)
処理A
jmp
処理B

パターン2
if(x!=y){
処理A
}
else{
処理B
}

cmp x,y
je (処理Bのアドレス)
処理A
jmp
処理B


パターン3
if(x>=y){
処理A
}
else{
処理B
}

cmp x,y
jl (処理Bのアドレス)
処理A
jmp
処理B

パターン4

if(x>y){
処理A
}
else{
処理B
}


cmp x,y
jle (処理Bのアドレス)
処理A
jmp
処理B


パターン5

if(x<=y){
処理A
}
else{
処理B
}


cmp x,y
jg (処理Bのアドレス)
処理A
jmp
処理B

よくよく考えれば、ジャンプ命令で分岐を制御してるんだから、Cコードと逆になるのは当然ですね。