日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747



在x86下能不能混著用寄存器呢?就比如 windows 上的 fastcall 調用協定,其實是可以的,就是在 mytest 方法上加 __attribute__((regparm(N))) 標記,這里的 N 不能超過 3 ,即參與傳遞的寄存器個數。

一:背景

1. 講故事

今天是五一的最后一天,想著長期都在 Windows 平臺上做開發,準備今天換到 Ubuntu 系統上體驗下,主要是想學習下 AT&T 風格的匯編,這里 Visual Studio 肯定是裝不了了,還得上 VSCode,剛好前幾天買了一個小工控機,這里簡單記錄下 零到一 的過程吧。

二:搭建一覽

1. VSCode 安裝

在 Ubuntu 上也有類似 Windows 的微軟商店的 軟件市場,可以在商店中直接安裝。

圖片

既然要換體驗,那就多用命令的方式安裝吧。

sudo apt update

sudo apt install software-properties-common apt-transport-https wget

wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"

sudo apt install code

code

2. gcc 安裝

由于 ubuntu 自帶了 gcc,g++,gdb 所以這一塊大家不需要操心,可以用 -v 觀察各自的版本。

skyfly@skyfly-virtual-machine:~/Desktop$ g++ -v
nux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 

skyfly@skyfly-virtual-machine:~/Desktop$ gdb -v
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2

3. 配置 vscode

為了能夠讓 vscode 跑 C++ 程序,先配置下 launch.json 文件。

// An highlighted block
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

再配置下 tasks.json 文件。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-std=c++11",
                "-o",
                "${fileBasenameNoExtension}.out"
            ]
        }
    ]
}

然后在 VSCode 面板中安裝下 GDB Debug 和 C/C++ Extension Pack 兩個插件,其他都是附帶上去的,截圖如下:

圖片

3. 一個簡單的程序測試

為了方便體驗 AT&T 風格,寫一個多參數的方法,順帶觀察寄存器傳值。

#include <IOStream>

using namespace std;

int mytest(int a, int b, int c, int d, int e, int f, int g)
{
    printf("a=%d,b=%d,c=%d,d=%d,e=%d,f=%d,g=%d", a, b, c, d, e, f, g);

    return 0;
}

int main()
{
    int a = 10;
    int b = 11;
    int c = 12;
    int d = 13;
    int e = 14;
    int f = 15;
    int g = 16;

    mytest(a,b,c,d,e,f,g);
}

在 mytest 方法下一個斷點,然后在 DEBUG CONSOLE 窗口輸入 -exec disassemble /m 就能看到本方法的匯編代碼,截圖如下:

圖片

仔細觀察上圖,可以看到 mytest 方法的前六個參數依次使用了 edi, esi, edx, ecx, r8d, r9d 寄存器,雖然都是 X64 調用協定,和 Windows 平臺的4個寄存器有明顯不同哈。

既然都看了默認的x64,不看 x86 的傳遞就有點遺憾哈,要想編譯成 32bit 的,需要做一些簡單配置。

$ sudo apt-get install build-essential module-assistant  
$ sudo apt-get install gcc-multilib g++-multilib

然后在 g++ 編譯時增加 -m32 參數,在 tasks.json 中增加即可。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-m32",
                "${file}",
                "-std=c++11",
                "-o",
                "${fileBasenameNoExtension}.out"
            ]
        }
    ]
}

接下來觀察下匯編代碼,可以發現走的都是 棧空間。

24     mytest(a,b,c,d,e,f,g);
=> 0x565562a2 <+80>: sub    $0x4,%esp
   0x565562a5 <+83>: pushl  -0xc(%ebp)
   0x565562a8 <+86>: pushl  -0x10(%ebp)
   0x565562ab <+89>: pushl  -0x14(%ebp)
   0x565562ae <+92>: pushl  -0x18(%ebp)
   0x565562b1 <+95>: pushl  -0x1c(%ebp)
   0x565562b4 <+98>: pushl  -0x20(%ebp)
   0x565562b7 <+101>: pushl  -0x24(%ebp)
   0x565562ba <+104>: call   0x5655620d <mytest(int, int, int, int, int, int, int)>
   0x565562bf <+109>: add    $0x20,%esp

還有一個問題,在x86下能不能混著用寄存器呢?就比如 windows 上的 fastcall 調用協定,其實是可以的,就是在 mytest 方法上加 __attribute__((regparm(N))) 標記,這里的 N 不能超過 3 ,即參與傳遞的寄存器個數,修改后如下:

__attribute__((regparm(3)))
int mytest(int a, int b, int c, int d, int e, int f, int g)
{
    printf("a=%d,b=%d,c=%d,d=%d,e=%d,f=%d,g=%d", a, b, c, d, e, f, g);

    return 0;
}

然后把程序跑起來再次觀察,很明顯的看到這次用了 eax, edx, ecx 來傳遞方法的前三個參數,匯編代碼如下:

24     mytest(a,b,c,d,e,f,g);
=> 0x565562aa <+80>: mov    -0x1c(%ebp),%ecx
   0x565562ad <+83>: mov    -0x20(%ebp),%edx
   0x565562b0 <+86>: mov    -0x24(%ebp),%eax
   0x565562b3 <+89>: pushl  -0xc(%ebp)
   0x565562b6 <+92>: pushl  -0x10(%ebp)
   0x565562b9 <+95>: pushl  -0x14(%ebp)
   0x565562bc <+98>: pushl  -0x18(%ebp)
   0x565562bf <+101>: call   0x5655620d <mytest(int, int, int, int, int, int, int)>
   0x565562c4 <+106>: add    $0x10,%esp

三:總結

習慣了 Intel 風格的匯編,再看 AT&T 風格的會極度不舒服,簡直是逆天哈,感覺都是反方向的,相信熟悉一段時間之后就好了,本篇的一個簡單搭建,希望對你有幫助。

分享到:
標簽:編程
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定