玩一玩 Ubuntu 下的 VSCode 編程
一:背景
1. 講故事
今天是五一的最后一天,想著長期都在 Windows 平臺上做開發(fā),準備今天換到 Ubuntu 系統(tǒng)上體驗下,主要是想學(xué)習(xí)下 AT&T 風(fēng)格的匯編,這里 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
code2. 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.23. 配置 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 風(fēng)格,寫一個多參數(shù)的方法,順帶觀察寄存器傳值。
#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 方法的前六個參數(shù)依次使用了 edi, esi, edx, ecx, r8d, r9d 寄存器,雖然都是 X64 調(diào)用協(xié)定,和 Windows 平臺的4個寄存器有明顯不同哈。
既然都看了默認的x64,不看 x86 的傳遞就有點遺憾哈,要想編譯成 32bit 的,需要做一些簡單配置。
$ sudo apt-get install build-essential module-assistant  
$ sudo apt-get install gcc-multilib g++-multilib然后在 g++ 編譯時增加 -m32 參數(shù),在 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"
            ]
        }
    ]
}接下來觀察下匯編代碼,可以發(fā)現(xiàn)走的都是 棧空間。
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 調(diào)用協(xié)定,其實是可以的,就是在 mytest 方法上加 __attribute__((regparm(N))) 標(biāo)記,這里的 N 不能超過 3 ,即參與傳遞的寄存器個數(shù),修改后如下:
__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 來傳遞方法的前三個參數(shù),匯編代碼如下:
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三:總結(jié)
習(xí)慣了 Intel 風(fēng)格的匯編,再看 AT&T 風(fēng)格的會極度不舒服,簡直是逆天哈,感覺都是反方向的,相信熟悉一段時間之后就好了,本篇的一個簡單搭建,希望對你有幫助。















 
 
 









 
 
 
 