ã¯ããã«
ãããã¾ã§ããã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ã§å¦ã¶ã¢ã»ã³ããªè¨èªãã®ç¬¬1åã¨ç¬¬2åãéãã¦ãMicrosoft Visual C++ã®ã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ãç¨ããã¢ã»ã³ããªè¨èªã®åºæ¬ã«ã¤ãã¦èª¬æãã¦ãã¾ããã
ã第3åã¨ãªãæ¬ç¨¿ã§ã¯ãã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ãç¨ããæµãå¶å¾¡ã解説ãã¾ããã¢ã»ã³ããªè¨èªã¯é«æ°´æºè¨èªã«ãããæã®æ¦å¿µããªãããã¹ã¦ã®å½ä»¤ãåç´ãªãªãã³ã¼ãã¨å¼æ°ï¼ãªãã©ã³ãï¼ã ãã§æ§æããã¦ãã¾ãããã®ç´ç²ãªæ§é ã®ããã«ãæµãå¶å¾¡ãè¡ãå ´åããif
æãfor
æã®ããã«è¨è¿°ãããã¨ã¯ã§ãã¾ãããåºæ¬çã«ããã°ã©ã ã®æµãå¶å¾¡ã¯ãã¹ã¦Cè¨èªã§è¨ãã¨ããã®goto
æã ãã§è¨è¿°ããæ¯è¼å½ä»¤ãæ¡ä»¶ã¸ã£ã³ããåå¥ã®å½ä»¤ã§è¡ãã¾ãã
ãæ¬æ¥ã®ãã¯ãæ©è½ãæããªãçã®ã¢ã»ã³ããªè¨èªãªãã°ãã¸ã£ã³ãå ã®ããã°ã©ã ã®ä½ç½®ãããããã¹ã¦ã¡ã¢ãªã¢ãã¬ã¹ã§æå®ããªããã°ãªãã¾ãããå®éã«ãæ©æ¢°èªã«ã¢ã»ã³ãã«ãããå¾ã¯ãã©ãã«ã¨ããæ å ±ã¯æããããã¹ã¦ã®ã¸ã£ã³ãå¦çã¯ãããã°ã©ã ããã¼ãããã¦ããã¡ã¢ãªã¢ãã¬ã¹ãè¨ç®ãã¦æå®ãã¾ãã
ãããããªãããããããä½æ¥ããã¹ã¦æè¨ç®ã§è¡ãã®ã¯å°é£ã§ããå¤æ°ã®ã¡ã¢ãªãµã¤ãºã®è¨ç®ã¨ã¯ç°ãªããã¢ã»ã³ããªè¨èªãæ©æ¢°èªã«ã¢ã»ã³ãã«ãããå¾ã®ãªãã³ã¼ãã¨ãªãã©ã³ãã®ãµã¤ãºãããç®çã®ã³ã¼ããé ç½®ããã¦ããã¢ãã¬ã¹ãè¨ç®ããä½æ¥ã¯ç ©éã§ãããééãã許ããã¾ãããããããä½æ¥ã¯äººéã§ã¯ãªãããã¯ããã¯ããã³ã³ãã¤ã©ã«å§ããã¹ãä»äºã§ãã
ã幸ãã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ã§ã¯ãCè¨èªã®ã©ãã«ãç¨ãã¦goto
æã®ããã«ç®çã®ã³ã¼ãã«ç§»åãããã¨ãã§ãã¾ããæ¬ç¨¿ã§ãæ©æ¢°èªã«æãè¿ãã¢ã»ã³ããªè¨èªãã©ã®ãããªå½ä»¤ã§ããã°ã©ã ã®æµããå¶å¾¡ããã®ããä½é¨ãããã¨ãã§ãã¾ãã
åç´ãªã¸ã£ã³ã
ãæ¡ä»¶ãæå®ããã«ãç¡æ¡ä»¶ã«æå®ããå ´æã«ç§»åããã«ã¯JMP
å½ä»¤ã使ãã¾ãã
jmp dest
ãåè¿°ããã¨ãããæ¬æ¥ã®JMP
å½ä»¤ã¯ç§»åå
ã®ã¡ã¢ãªã¢ãã¬ã¹ãæå®ãããã®ã§ãããã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ã§ã¯Cè¨èªã®ã©ãã«ãæå®ãããã¨ãã§ãã¾ããJMP
å½ä»¤ã®ãã£ã¹ãã£ãã¼ã·ã§ã³ã»ãªãã©ã³ãã«ã¯ãCè¨èªã®ã©ãã«ãæå®ãã¦ãã ãããã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ã®ãããã¯å
ã«ã©ãã«ãæå®ãããã¨ãã§ããã®ã§ãCè¨èªã®ä»»æã®ã³ã¼ããããã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ã®ã³ã¼ãã«ç§»åãããã¨ãã§ãã¾ãã
ãæ©æ¢°èªã®ä¸çã§ã¯ããIPãã¨ããç¹æ®ãªã¬ã¸ã¹ã¿ã«ãå®è¡ããã¹ãããã°ã©ã ã®ã¢ãã¬ã¹ãæ ¼ç´ããã¦ãã¾ããCPUã¯ããã°ã©ã ãå®è¡ãããã¨ã«IPã¬ã¸ã¹ã¿ã®å¤ã次ã®å½ä»¤ã«ç§»åããã¾ããJMP
å½ä»¤ã¯ãæ§è³ªçã«IPã¬ã¸ã¹ã¿ã®å¤ãå¤æ´ããå½ä»¤ã§ããã¨è§£éãããã¨ãã§ãã¾ãã
#include <stdio.h> int main() { int value = 0; __asm { jmp label ;label ã©ãã«ã¾ã§ã¸ã£ã³ããã add value, 100 ;ãã®å½ä»¤ãé£ã°ããã label: add value, 1000; } printf("value=%d\n", value); return 0; }
value=1000
ããsample01ãã¯ãå¤0ã§åæåããæ´æ°åã®å¤æ°value
ãç¨æãã¦ãã¾ãã__asm
ãããã¯ã§value
ã«100ãå ç®ããã³ã¼ãã¨ã1000ãå ç®ããã³ã¼ããç¨æãã¦ãã¾ãããJMP
å½ä»¤ãå®è¡ãããæç¹ã§label
ã¿ã°ã«ç¡æ¡ä»¶ã«ã¸ã£ã³ããã¾ãããã®ããadd value, 100
ãå®è¡ããããããã°ã©ã ã®çµæã¯1000ã¨ãªãã¾ããJMP
å½ä»¤ãã³ã¡ã³ãåãã¦å®è¡ããªãããã«ããã°ãç´å¾ã®ADD
å½ä»¤ãå®è¡ããã¦1100ã¨ããå®è¡çµæã«ãªããã¨ãåããã¾ãã
æ¡ä»¶åå²
ãç¹å®ã®å¤ã調ã¹ããã®çµæã«ãã£ã¦ããã°ã©ã ã®æµããåå²ãããã«ã¯ãæåã«CMP
å½ä»¤ãå®è¡ãã¾ããCMP
å½ä»¤ã¯ãªãã©ã³ããæ¯è¼ãã¦ãã®çµæãã¬ã¸ã¹ã¿ã«ä¿åãã¾ãã
cmp src1 , src2
ããã®å½ä»¤ã¯ã2ã¤ã®ã½ã¼ã¹ã»ãªãã©ã³ããæã¡ã¾ããsrc1
ã«ç¬¬1ã½ã¼ã¹ã»ãªãã©ã³ãããsrc2
ã«ç¬¬2ã½ã¼ã¹ã»ãªãã©ã³ããæå®ãã¾ããããããã®ã½ã¼ã¹ã»ãªãã©ã³ãã«ã¯ãã¬ã¸ã¹ã¿ãã¡ã¢ãªã¢ãã¬ã¹ãã¾ãã¯å³å¤ã®ãããããæå®ãã¾ãã
ãCMP
å½ä»¤ã¯ãæå®ããã½ã¼ã¹ã»ãªãã©ã³ããæ¯è¼ããEFLAGS
ã¨å¼ã°ããã¬ã¸ã¹ã¿ã«çµæãä¿åãã¾ããEFLAGS
ã¯ãå½ä»¤ãå®è¡ããçµæã®å¯ä½ç¨çãªæ
å ±ããã©ã°ã¨ãã¦ä¿åããã¬ã¸ã¹ã¿ã§ãããã®ã¬ã¸ã¹ã¿ã®å¤ã¯ãç´æ¥èªã¿æ¸ãããã¦å©ç¨ãããã®ã§ã¯ãªããå½ä»¤ã®çµæã¨ãã¦è¨å®ãããããå½ä»¤ã®æ¡ä»¶ã¨ãã¦å©ç¨ããã¾ãã
ãCMP
å½ä»¤ãå®è¡ãããã¨EFLAGS
ã¬ã¸ã¹ã¿ã®ZF
ã¨å¼ã°ãããã©ã°ãè¨å®ããã¾ããZF
ã¨ã¯ãã¼ããã©ã°ã®ãã¨ã§ãæ¼ç®ã®çµæã0ã§ããã°ã»ãããããããã§ãªããã°è§£é¤ãããã¨ããæ§è³ªãæã£ã¦ãã¾ããCMP
å½ä»¤ã¯ãä¸ç¨®ã®æ¸ç®å¦çãå
é¨ã§è¡ãã¾ããããã®çµæã¯ç ´æ£ããã¾ããæ¡ä»¶å¶å¾¡ã¯ããã®æ¼ç®ã«ãã£ã¦å¾ãããçµæã使ã£ã¦ã¸ã£ã³ãããããã¨ã§å®ç¾ã§ãã¾ããCMP
å½ä»¤ã¯ä¸ç¨®ã®æ¸ç®ãªã®ã§ãåæ¹ã«æå®ãããªãã©ã³ãã®å¤ãä¸è´ãã¦ããã°ãçµæã0ã¨ãªãZF
ãã©ã°ãã»ãããããã¨ããä»çµã¿ã«ãªãã¾ãã
ãZF
ãã©ã°ã®ç¶æ
ã«ãã£ã¦ã¸ã£ã³ããããã©ããã決å®ããå½ä»¤ã¯JZ
å½ä»¤ã¨ãJNZ
å½ä»¤ãããã¾ãã
jz dest
jnz dest
ãJZ
å½ä»¤ã¯ãZF
ãã©ã°ãã»ããããã¦ããå ´åã«ããã£ã¹ãã£ãã¼ã·ã§ã³ã»ãªãã©ã³ãã«æå®ãããå ´æã«ã¸ã£ã³ããã¾ããéã«JNZ
å½ä»¤ã¯ãZF
ãã©ã°ãã»ããããã¦ããªãå ´åã«ããã£ã¹ãã£ãã¼ã·ã§ã³ã»ãªãã©ã³ãã«æå®ãããå ´æã«ã¸ã£ã³ããã¾ãã
ãåç´ã«èª¬æãããªãã°ãããå¤æ°Aã¨Bãçãããã©ããã調ã¹ã¦ããã®çµæã«å¿ãã¦å®è¡ããã³ã¼ããå¤æ´ãããå ´åãCMP
å½ä»¤ã§Aã¨Bãæ¯è¼ãããã®å¾ã«JZ
å½ä»¤ãç¨ããå½¢ã«ãªãã§ãããã
#include <stdio.h> int main() { int value; char * tc = "value 㯠100 ã§ãã\n"; char * fc = "value 㯠100 ã§ã¯ããã¾ããã§ãã\n"; char * result; printf("value å¤æ°ã«ä»£å ¥ããä»»æã®å¤ãå ¥åãã¦ãã ãã>"); scanf("%d", &value); __asm { cmp value, 100 ;value ã 100 ã¨çãããã©ããã調ã¹ã jz cmp_true ;value ã 100 ãªãã¸ã£ã³ããã mov ebx, fc; mov result, ebx; jmp end ;end ã«ã¸ã£ã³ããã cmp_true: mov ebx, tc; mov result, ebx; end: } printf(result); return 0; }
C:\...>sample02 value å¤æ°ã«ä»£å ¥ããä»»æã®å¤ãå ¥åãã¦ãã ãã>10 value 㯠100 ã§ã¯ããã¾ããã§ãã C:\...>sample02 value å¤æ°ã«ä»£å ¥ããä»»æã®å¤ãå ¥åãã¦ãã ãã>100 value 㯠100 ã§ãã
ããsample02ãã¯ãæåã«scanf()
é¢æ°ãç¨ãã¦ã¦ã¼ã¶ã¼ã«value
å¤æ°ã«ä»£å
¥ããå¤ãå
¥åãã¦ãããã¾ãããã®å¾__asm
ãããã¯å
ã§value
å¤æ°ã®å¤ã調ã¹ãå
¥åãããå¤ã100ã¨çãããã©ããã§ãçµæã¨ãã¦åºåããå¤æ°ãè¨å®ãã¾ããvalue
å¤æ°ã«å
¥åãããå¤ã100ã¨çãããã°JZ
å½ä»¤ã«ãã£ã¦cmp_true
ã©ãã«ã¾ã§ç§»åããtc
å¤æ°ãresult
å¤æ°ã«è¨å®ããã¾ããããã§ãªããã°ãfc
å¤æ°ãresult
å¤æ°ã«è¨å®ãããä¸é¨ã®tc
ãresult
ã«è¨å®ããããã°ã©ã ããã®ã¾ã¾å®è¡ãããªãããã«ããããã«JMP
å½ä»¤ã§end
ã©ãã«ã¾ã§ç§»åãã¦ãã¾ãããããã®ä½æ¥ã¯ãCè¨èªã®if-else
æã¨åãã§ãã
ãJZ
å½ä»¤ãJNZ
å½ä»¤ã§ã¯ãå¤ãçãããã©ããã調ã¹ã¦ã¸ã£ã³ããã¦ãã¾ããããå½ç¶ãå¤ãããå°ããããã¾ãã¯ãã大ãããã調ã¹ããã¨ãå¯è½ã§ããããããå¤å®å¦çãJZ
å½ä»¤ã¨åæ§ã«CMP
å½ä»¤ã«ããçµæãEFLAGS
ã¬ã¸ã¹ã¿ã«ä¿åãããEFLAGS
ã¬ã¸ã¹ã¿å
ã®å種ãã©ã°ã®å¤ã調ã¹ã¦å®è¡ããã¾ããã¸ã£ã³ãå½ä»¤ã¯é常ã«æ°ãå¤ãã®ã§è©³ç´°ã¯å²æãã¾ããã使ãæ¹ã¯JZ
å½ä»¤ã¨åãã§ãã
å½ä»¤ | ã¸ã£ã³ãæ¡ä»¶ |
JA | ããä¸ï¼CF = 0 & ZF = 0ï¼ |
JAE | ããä¸ãçããï¼CF = 0ï¼ |
JB | ããä¸ï¼CF = 1ï¼ |
JBE | ããä¸ãçããï¼CF = 1 | ZF = 1ï¼ |
JC | ãã£ãªã¼ãããï¼CF = 1ï¼ |
JCXZ | CXã¬ã¸ã¹ã¿ã0 |
JE | çãã(ZF = 1ï¼ |
JG | ãã大ããï¼ZF = 0 & SF = OFï¼ |
JGE | ãã大ãããçããï¼SD = OFï¼ |
JL | ããå°ããï¼SF ! OFï¼ |
JLE | ããå°ãããçããï¼ZF = 1 | SF ! OFï¼ |
JNA | ããä¸ã§ãªãï¼CF = 1 | ZF = 1ï¼ |
JNAE | ããä¸ã§ãªãçããï¼CF = 1ï¼ |
JNB | ããä¸ã§ãªãï¼CF = 0ï¼ |
JNBE | ããä¸ã§ãªãçããï¼CF = 0 & ZF = 0ï¼ |
JNC | ãã£ãªã¼ããªãï¼CF = 0ï¼ |
JNE | çãããªã(ZF = 0ï¼ |
JNG | ãã大ãããªãï¼ZF = 1 | SF ! OFï¼ |
JNGE | ãã大ãããªãçãããªãï¼SF ! OFï¼ |
JNL | ããå°ãããªãï¼SF = OFï¼ |
JNLE | ããå°ãããªãçãããªã(ZF = 0 & SF = OFï¼ |
JNO | ãªã¼ãã¼ããã¼ããªãï¼OF = 0ï¼ |
JNP | ããªãã£ããªãï¼PF = 0ï¼ |
JNS | 符å·ããªãï¼SF = 0ï¼ |
JNZ | ã¼ãã§ã¯ãªãï¼ZF = 0ï¼ |
JO | ãªã¼ãã¼ããã¼ãããï¼PF = 1ï¼ |
JP | ããªãã£ãããï¼PF = 1ï¼ |
JPE | ããªãã£ãå¶æ°(PF = 1ï¼ |
JPO | ããªãã£ãåºæ°(PF = 0ï¼ |
JS | 符åããã(SF = 1ï¼ |
JZ | ã¼ãã§ãã(ZF = 1ï¼ |
ãä¸ã®è¡¨ã«ç¤ºããå種ã¸ã£ã³ãå½ä»¤ã¯ãæ¼ç®ã®çµæã«ãã£ã¦è¨å®ãããEFLAGS
ã¬ã¸ã¹ã¿ã®ãã©ã°ã調ã¹ã¦ããã®çµæãçã§ããã°ã¸ã£ã³ãããã¨ããæ§è³ªãæã¡ã¾ããé常ã¯ãCMP
å½ä»¤ã§å¤ãæ¯è¼ããå¾ãã¸ã£ã³ãå½ä»¤ã§æ¡ä»¶ã«å¾ã£ã¦ã¸ã£ã³ããããããªããã決å®ããã¨ãã使ãæ¹ã«ãªãã¾ãã
#include <stdio.h> int main() { int a, b; char * r1 = "a 㯠b ãã大ãã\n"; char * r2 = "a 㯠b ããå°ãã\n"; char * r3 = "a 㯠b ã¨çãã\n"; char * result; printf("a ã®å¤ãå ¥åãã¦ãã ãã>"); scanf("%d", &a); printf("b ã®å¤ãå ¥åãã¦ãã ãã>"); scanf("%d", &b); __asm { mov eax, a; cmp eax, b; ja label1; jb label2; mov ebx, r3; mov result, ebx; jmp end; label1: mov ebx, r1; mov result, ebx; jmp end; label2: mov ebx, r2; mov result, ebx; end: } printf(result); return 0; }
C:\...>sample03 a ã®å¤ãå ¥åãã¦ãã ãã>10 b ã®å¤ãå ¥åãã¦ãã ãã>100 a 㯠b ããå°ãã a ã®å¤ãå ¥åãã¦ãã ãã>100 b ã®å¤ãå ¥åãã¦ãã ãã>10 a 㯠b ãã大ãã a ã®å¤ãå ¥åãã¦ãã ãã>100 b ã®å¤ãå ¥åãã¦ãã ãã>100 a 㯠b ã¨çãã
ããsample03ãã¯ãscanf()
é¢æ°ããã¦ã¼ã¶ã¼ã«å¤æ°a
ã¨b
ã®å¤ãå
¥åãã¦ããããa
ã¨b
ã®å¤ãæ¯è¼ãã¦ããã®çµæã«å¿ããæååãåºåããã¨ããããã°ã©ã ã§ããCMP
å½ä»¤ã§a
ã¨b
ãæ¯è¼ãããã®å¾JA
å½ä»¤ãã¾ãã¯JB
å½ä»¤ã§ãç®çã®ã³ã¼ãã«ã¸ã£ã³ããã¾ãã