天天看點

彙編cmp指令

 cmp是比較指令,cmp的功能相當于減法指令(sub)。它不儲存結果,隻是影響相應的标志位。其他的指令通過識别這些被影響的标志位來得知比較結果。

     cmp指令格式:   cmp   操作對象1, 操作對象2

     注意是計算 操作對象2 - 操作對象1,和sub的一樣,而不是 操作數1-操作數2(ATT格式), 但不儲存結果,隻是根據結果修改相應的标志位。

附我自己寫的測試用彙編:

#include <stdio.h> int main() { int eax = 100; int ebx = 200; int output = - 100; asm volatile( "movl % 1,%%eax;\n\t" "movl % 2,%%ebx;\n\t" "cmpl %%eax,%%ebx;\n\t" //用的是cmpl比較指令 "sets %%al;\n\t" //負數時設定 "movzbl %%al,%%eax;\n\t" "movl %%eax, % 0\n\t" : "=m"(output) : "r"(eax), "r"(ebx) : "eax"); printf( "%d \n", output); return 0; } 最終的輸出是1

cmp

繼續閱讀