C語言的位運算操作包括兩類,邏輯運算操作和邏輯移位操作,
邏輯運算操作
C語言提供了四種按位邏輯運算子,分別是按位取反,按位與,按位或,按位異或,在編譯時,編譯器會根據運算元的寬度分別轉換為不同的指令,
| 操作 | C語言運算子 | 匯編指令 |
|---|---|---|
| 按位取反 | ~ | notb、notw、notl |
| 按位與 | & | andb、andw、andl |
| 按位或 | l | orb、orw、orl |
| 按位異或 | ^ | xorb、xorw、xorl |
注意: C語言的邏輯與(&&)、邏輯或(||)、邏輯非(!)并沒有對應的機器指令,而是由多條指令聯合來實作這些功能,完成以變數為單位的邏輯操作,
下面我們以一個簡單的C語言程式test.c來了解邏輯運算操作程序,
#include <stdio.h>
void main()
{
int a=5;
unsigned int b=3;
short c=5;
int d=0;
a = ~a;
b = ~b;
c = ~c;
d = a&b;
d = a^b;
d = a|b;
return;
}
利用gcc命令將其進行編譯成可執行檔案,
gcc -o0 -m32 -g test.c -o test
利用objdump命令進行反匯編并將其重定向到test.txt檔案方便查看,
objdump -S test>test.txt
main函式所對應的匯編指令如下所示,
000004ed <main>:
#include <stdio.h>
void main()
{
4ed: 55 push %ebp
4ee: 89 e5 mov %esp,%ebp
4f0: 83 ec 10 sub $0x10,%esp
4f3: e8 48 00 00 00 call 540 <__x86.get_pc_thunk.ax>
4f8: 05 e4 1a 00 00 add $0x1ae4,%eax
int a=5;
4fd: c7 45 f4 05 00 00 00 movl $0x5,-0xc(%ebp)
unsigned int b=3;
504: c7 45 f8 03 00 00 00 movl $0x3,-0x8(%ebp)
short c=5;
50b: 66 c7 45 f2 05 00 movw $0x5,-0xe(%ebp)
int d=0;
511: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp)
a = ~a;
518: f7 55 f4 notl -0xc(%ebp)
b = ~b;
51b: f7 55 f8 notl -0x8(%ebp)
c = ~c;
51e: 66 f7 55 f2 notw -0xe(%ebp)
d = a&b;
522: 8b 45 f4 mov -0xc(%ebp),%eax
525: 23 45 f8 and -0x8(%ebp),%eax
528: 89 45 fc mov %eax,-0x4(%ebp)
d = a^b;
52b: 8b 45 f4 mov -0xc(%ebp),%eax
52e: 33 45 f8 xor -0x8(%ebp),%eax
531: 89 45 fc mov %eax,-0x4(%ebp)
d = a|b;
534: 8b 45 f4 mov -0xc(%ebp),%eax
537: 0b 45 f8 or -0x8(%ebp),%eax
53a: 89 45 fc mov %eax,-0x4(%ebp)
return;
53d: 90 nop
}
53e: c9 leave
53f: c3 ret
由以上代碼可以看出a,b,c取反的三個操作分別對應以下指令,
a = ~a;
518: f7 55 f4 notl -0xc(%ebp)
b = ~b;
51b: f7 55 f8 notl -0x8(%ebp)
c = ~c;
51e: 66 f7 55 f2 notw -0xe(%ebp)
其中變數a和變數b的取反指令都是notl,處理的是4位元組的變數,而變數c的取反指令執行的是notw,執行的是2位元組的變數,這也就說明了編譯器會根據運算元的寬度分別轉換為不同的指令,
下表給出C語言基本資料和型別和IA-32運算元型別的對應關系
| C語言宣告 | 匯編指令長度后綴 | 存盤長度 |
|---|---|---|
| (unsigned) char | b | 8 |
| (unsigned) short | w | 16 |
| (unsigned) int | l | 32 |
| (unsigned) long int | l | 32 |
| (unsigned) long long int | - | 2 $\times$ 32 |
| char * | l | 32 |
| float | s | 32 |
| double | l | 64 |
| long double | t | 80/96 |
仍然以下面這樣一個簡單的C語言程式來理解邏輯與(&&)、邏輯或(||)、邏輯非(!)和按位邏輯運算子的區別,
#include <stdio.h>
void main()
{
int a=5;
unsigned int b=3;
short c=5;
int d=0;
a = !a;
b = !b;
c = !c;
d = a&&b;
d = a||b;
return;
}
利用gcc命令將其進行編譯,objdump命令進行反匯編之后,main函式所對應的匯編指令如下所示,
000004ed <main>:
#include <stdio.h>
void main()
{
4ed: 55 push %ebp
4ee: 89 e5 mov %esp,%ebp
4f0: 83 ec 10 sub $0x10,%esp
4f3: e8 82 00 00 00 call 57a <__x86.get_pc_thunk.ax>
4f8: 05 e4 1a 00 00 add $0x1ae4,%eax
int a=5;
4fd: c7 45 f4 05 00 00 00 movl $0x5,-0xc(%ebp)
unsigned int b=3;
504: c7 45 f8 03 00 00 00 movl $0x3,-0x8(%ebp)
short c=5;
50b: 66 c7 45 f2 05 00 movw $0x5,-0xe(%ebp)
int d=0;
511: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp)
a = !a;
518: 83 7d f4 00 cmpl $0x0,-0xc(%ebp)
51c: 0f 94 c0 sete %al
51f: 0f b6 c0 movzbl %al,%eax
522: 89 45 f4 mov %eax,-0xc(%ebp)
b = !b;
525: 83 7d f8 00 cmpl $0x0,-0x8(%ebp)
529: 0f 94 c0 sete %al
52c: 0f b6 c0 movzbl %al,%eax
52f: 89 45 f8 mov %eax,-0x8(%ebp)
c = !c;
532: 66 83 7d f2 00 cmpw $0x0,-0xe(%ebp)
537: 0f 94 c0 sete %al
53a: 0f b6 c0 movzbl %al,%eax
53d: 66 89 45 f2 mov %ax,-0xe(%ebp)
d = a&&b;
541: 83 7d f4 00 cmpl $0x0,-0xc(%ebp)
545: 74 0d je 554 <main+0x67>
547: 83 7d f8 00 cmpl $0x0,-0x8(%ebp)
54b: 74 07 je 554 <main+0x67>
54d: b8 01 00 00 00 mov $0x1,%eax
552: eb 05 jmp 559 <main+0x6c>
554: b8 00 00 00 00 mov $0x0,%eax
559: 89 45 fc mov %eax,-0x4(%ebp)
d = a||b;
55c: 83 7d f4 00 cmpl $0x0,-0xc(%ebp)
560: 75 06 jne 568 <main+0x7b>
562: 83 7d f8 00 cmpl $0x0,-0x8(%ebp)
566: 74 07 je 56f <main+0x82>
568: b8 01 00 00 00 mov $0x1,%eax
56d: eb 05 jmp 574 <main+0x87>
56f: b8 00 00 00 00 mov $0x0,%eax
574: 89 45 fc mov %eax,-0x4(%ebp)
return;
機器指令邏輯非(!)實作的操作解釋,以a = !a這個作為例子:
a = !a;
518: 83 7d f4 00 cmpl $0x0,-0xc(%ebp)
51c: 0f 94 c0 sete %al
51f: 0f b6 c0 movzbl %al,%eax
522: 89 45 f4 mov %eax,-0xc(%ebp)
首先將變數a與常數0進行比較,如果相等就置暫存器al為1,不等則置為0,然后再把暫存器al的值擴展0擴展送到eax暫存器中,再從暫存器eax中送回到變數a的地址當中,
機器指令邏輯與(&&)實作的操作解釋,以d = a&&b來解釋,
d = a&&b;
541: 83 7d f4 00 cmpl $0x0,-0xc(%ebp)
545: 74 0d je 554 <main+0x67>
547: 83 7d f8 00 cmpl $0x0,-0x8(%ebp)
54b: 74 07 je 554 <main+0x67>
54d: b8 01 00 00 00 mov $0x1,%eax
552: eb 05 jmp 559 <main+0x6c>
554: b8 00 00 00 00 mov $0x0,%eax
559: 89 45 fc mov %eax,-0x4(%ebp)
首先將變數a與0進行相比,如果變數a等于0,就跳到554這個位置,也就是執行指令mov $0x0,%eax,就是把0送到暫存器eax里面,再送到變數d當中,如果變數a不等于0,就用變數b與0相比,如果b等于0,也是跳轉到554這個位置去將最終的結果設定為0,如果變數b也不等于0,就把1送到暫存器eax當中,將最終的結果設定為1,
機器指令邏輯或(||)實作的操作解釋,以d = a||b來解釋
d = a||b;
55c: 83 7d f4 00 cmpl $0x0,-0xc(%ebp)
560: 75 06 jne 568 <main+0x7b>
562: 83 7d f8 00 cmpl $0x0,-0x8(%ebp)
566: 74 07 je 56f <main+0x82>
568: b8 01 00 00 00 mov $0x1,%eax
56d: eb 05 jmp 574 <main+0x87>
56f: b8 00 00 00 00 mov $0x0,%eax
574: 89 45 fc mov %eax,-0x4(%ebp)
首先將變數a與0進行相比,如果變數a不等于0,就跳轉到558這個位置,也就是執行指令mov $0x1,%eax,把1送到暫存器eax里面,無條件轉到574這個位置,并將eax的值送到變數d當中,如果變數a等于0,就將變數b與0比較,如果b等于0,就跳轉到56f這個位置,去將最終的結果設定為0,
邏輯移位操作
C語言的移位操作包括邏輯左移,算術左移,邏輯右移,算術右移等四種,
| 操作 | C語言運算子 | 匯編指令 |
|---|---|---|
| 邏輯左移 | << | shlb、shlw、shll |
| 算術左移 | << | salb、salw、sall |
| 邏輯右移 | >> | shrb、shrw、shrl |
| 算術右移 | >> | sarb、sarw、sarl |
注意:IA-32中的其他移位指令沒有對應的C語言操作,如想實作回圈移位指令,需要撰寫多條陳述句來實作,
邏輯移位和算術移位的C語言運算子相同,編譯器會根據運算元的不同來選擇不同的指令,無符號數采用邏輯移位指令,有符號數采用算術移位指令,邏輯和算術的區別在于友移時最高位補0還是補符號位,算術右移補入符號位,邏輯右移補入0,
我們仍然以一個簡單的C語言指令來為大家介紹邏輯移位操作的匯編指令,
#include <stdio.h>
void main()
{
int a = 0x80000000;
unsigned int b = 0x80000000;
short c = 0x8000;
unsigned short d = 0x8000;
a=a>>4;
b=b>>4;
a=c;
a=d;
b=c;
b=d;
return;
}
利用gcc命令將其進行編譯,objdump命令進行反匯編之后,main函式所對應的匯編指令如下所示
000004ed <main>:
#include <stdio.h>
void main()
{
4ed: 55 push %ebp
4ee: 89 e5 mov %esp,%ebp
4f0: 83 ec 10 sub $0x10,%esp
4f3: e8 46 00 00 00 call 53e <__x86.get_pc_thunk.ax>
4f8: 05 e4 1a 00 00 add $0x1ae4,%eax
int a = 0x80000000;
4fd: c7 45 f8 00 00 00 80 movl $0x80000000,-0x8(%ebp)
unsigned int b = 0x80000000;
504: c7 45 fc 00 00 00 80 movl $0x80000000,-0x4(%ebp)
short c = 0x8000;
50b: 66 c7 45 f4 00 80 movw $0x8000,-0xc(%ebp)
unsigned short d = 0x8000;
511: 66 c7 45 f6 00 80 movw $0x8000,-0xa(%ebp)
a=a>>4;
517: c1 7d f8 04 sarl $0x4,-0x8(%ebp)
b=b>>4;
51b: c1 6d fc 04 shrl $0x4,-0x4(%ebp)
a=c;
51f: 0f bf 45 f4 movswl -0xc(%ebp),%eax
523: 89 45 f8 mov %eax,-0x8(%ebp)
a=d;
526: 0f b7 45 f6 movzwl -0xa(%ebp),%eax
52a: 89 45 f8 mov %eax,-0x8(%ebp)
b=c;
52d: 0f bf 45 f4 movswl -0xc(%ebp),%eax
531: 89 45 fc mov %eax,-0x4(%ebp)
b=d;
534: 0f b7 45 f6 movzwl -0xa(%ebp),%eax
538: 89 45 fc mov %eax,-0x4(%ebp)
return;
從sarl $0x4,-0x8(%ebp)這條指令可以清楚的看到當執行a右移4位的操作時,因為a是有符號數,所以執行的就是算術右移,對應的匯編指令sarl,而執行b右移時,因為b是無符號數,所以執行的是邏輯右移指令,對應匯編指令shrl,
a=c;
51f: 0f bf 45 f4 movswl -0xc(%ebp),%eax
523: 89 45 f8 mov %eax,-0x8(%ebp)
a=d;
526: 0f b7 45 f6 movzwl -0xa(%ebp),%eax
52a: 89 45 f8 mov %eax,-0x8(%ebp)
b=c;
52d: 0f bf 45 f4 movswl -0xc(%ebp),%eax
531: 89 45 fc mov %eax,-0x4(%ebp)
b=d;
534: 0f b7 45 f6 movzwl -0xa(%ebp),%eax
538: 89 45 fc mov %eax,-0x4(%ebp)
由這8條指令可以看出,在執行a=c的時候,執行的是符號擴展指令,z=d時執行的是零擴展指令,b=c時執行的是符號擴展指令,b=d時執行的是零擴展指令,因此我們可以看出,執行符號擴展還是零擴展是由等號右邊的變數型別決定的,與等號左邊的變數型別無關,
位運算的作用
- 可實作特定的功能:取特定位、保留特定位
- 周期短速度快:左移、右移可用于實作快速的整數乘、除法
- 可實作其他功能:原位交換
PS:交換變數a和變數b的值
普通方法
c = a; a = b; b = c;
位操作交換法
a = a^b; b = b^a; a = a^b;
位操作法原理:
b = b^(a^b) = b^a^b = b^b^a = a
a = (a^b)^(b^(a^b)) = a^b^b^a^b = b
以上內容就是本次我給大家分享的計算機系統基礎學習的筆記-資料的位運算操作,小編也是初次入門,有什么地方寫的不對的,還請多多指教,覺得還不錯的點個贊支持一下小編,你的肯定就是小編前進的動力,另外如果想了解更多計算機專業的知識和技巧的,獻上我的個人博客北徯,另外需要各種資料的童鞋,可以關注我的微信公眾號北徯,免費的PPT模板,各種資料等你來領,

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/98115.html
標籤:Linux
上一篇:Linux手動新建用戶
下一篇:inode和block簡介
