经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
玩一玩 Ubuntu 下的 VSCode 编程
来源:cnblogs  作者:一线码农  时间:2023/5/4 9:17:18  对本文有异议

一:背景

1. 讲故事

今天是五一的最后一天,想着长期都在 Windows 平台上做开发,准备今天换到 Ubuntu 系统上体验下,主要是想学习下 AT&T 风格的汇编,这里 Visual Studio 肯定是装不了了,还得上 VSCode,刚好前几天买了一个小工控机,这里简单记录下 零到一 的过程吧。

二:搭建一览

1. VSCode 安装

在 Ubuntu 上也有类似 Windows 的微软商店的 软件市场,可以在商店中直接安装。

既然要换体验,那就多用命令的方式安装吧。

  1. sudo apt update
  2. sudo apt install software-properties-common apt-transport-https wget
  3. wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
  4. sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
  5. sudo apt install code
  6. code

2. gcc 安装

由于 ubuntu 自带了 gcc,g++,gdb 所以这一块大家不需要操心,可以用 -v 观察各自的版本。

  1. skyfly@skyfly-virtual-machine:~/Desktop$ g++ -v
  2. nux-gnu --target=x86_64-linux-gnu
  3. Thread model: posix
  4. gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
  5. skyfly@skyfly-virtual-machine:~/Desktop$ gdb -v
  6. GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2

3. 配置 vscode

为了能够让 vscode 跑 C++ 程序,先配置下 launch.json 文件。

  1. // An highlighted block
  2. {
  3. // Use IntelliSense to learn about possible attributes.
  4. // Hover to view descriptions of existing attributes.
  5. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  6. "version": "0.2.0",
  7. "configurations": [
  8. {
  9. "name": "(gdb) Launch",
  10. "type": "cppdbg",
  11. "request": "launch",
  12. "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
  13. "args": [],
  14. "stopAtEntry": false,
  15. "cwd": "${workspaceFolder}",
  16. "environment": [],
  17. "externalConsole": true,
  18. "MIMode": "gdb",
  19. "preLaunchTask": "build",
  20. "setupCommands": [
  21. {
  22. "description": "Enable pretty-printing for gdb",
  23. "text": "-enable-pretty-printing",
  24. "ignoreFailures": true
  25. }
  26. ]
  27. }
  28. ]
  29. }

再配置下 tasks.json 文件。

  1. {
  2. // See https://go.microsoft.com/fwlink/?LinkId=733558
  3. // for the documentation about the tasks.json format
  4. "version": "2.0.0",
  5. "tasks": [
  6. {
  7. "label": "build",
  8. "type": "shell",
  9. "command": "g++",
  10. "args": [
  11. "-g",
  12. "${file}",
  13. "-std=c++11",
  14. "-o",
  15. "${fileBasenameNoExtension}.out"
  16. ]
  17. }
  18. ]
  19. }

然后在 VSCode 面板中安装下 GDB DebugC/C++ Extension Pack 两个插件,其他都是附带上去的,截图如下:

3. 一个简单的程序测试

为了方便体验 AT&T 风格,写一个多参数的方法,顺带观察寄存器传值。

  1. #include <iostream>
  2. using namespace std;
  3. int mytest(int a, int b, int c, int d, int e, int f, int g)
  4. {
  5. printf("a=%d,b=%d,c=%d,d=%d,e=%d,f=%d,g=%d", a, b, c, d, e, f, g);
  6. return 0;
  7. }
  8. int main()
  9. {
  10. int a = 10;
  11. int b = 11;
  12. int c = 12;
  13. int d = 13;
  14. int e = 14;
  15. int f = 15;
  16. int g = 16;
  17. mytest(a,b,c,d,e,f,g);
  18. }

mytest 方法下一个断点,然后在 DEBUG CONSOLE 窗口输入 -exec disassemble /m 就能看到本方法的汇编代码,截图如下:

仔细观察上图,可以看到 mytest 方法的前六个参数依次使用了 edi, esi, edx, ecx, r8d, r9d 寄存器,虽然都是 X64 调用协定,和 Windows 平台的4个寄存器有明显不同哈。

既然都看了默认的x64,不看 x86 的传递就有点遗憾哈,要想编译成 32bit 的,需要做一些简单配置。

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

然后在 g++ 编译时增加 -m32 参数,在 tasks.json 中增加即可。

  1. {
  2. // See https://go.microsoft.com/fwlink/?LinkId=733558
  3. // for the documentation about the tasks.json format
  4. "version": "2.0.0",
  5. "tasks": [
  6. {
  7. "label": "build",
  8. "type": "shell",
  9. "command": "g++",
  10. "args": [
  11. "-g",
  12. "-m32",
  13. "${file}",
  14. "-std=c++11",
  15. "-o",
  16. "${fileBasenameNoExtension}.out"
  17. ]
  18. }
  19. ]
  20. }

接下来观察下汇编代码,可以发现走的都是 栈空间

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

还有一个问题,在x86下能不能混着用寄存器呢?就比如 windows 上的 fastcall 调用协定,其实是可以的,就是在 mytest 方法上加 __attribute__((regparm(N))) 标记,这里的 N 不能超过 3 ,即参与传递的寄存器个数,修改后如下:

  1. __attribute__((regparm(3)))
  2. int mytest(int a, int b, int c, int d, int e, int f, int g)
  3. {
  4. printf("a=%d,b=%d,c=%d,d=%d,e=%d,f=%d,g=%d", a, b, c, d, e, f, g);
  5. return 0;
  6. }

然后把程序跑起来再次观察,很明显的看到这次用了 eax, edx, ecx 来传递方法的前三个参数,汇编代码如下:

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

三:总结

习惯了 Intel 风格的汇编,再看 AT&T 风格的会极度不舒服,简直是逆天哈,感觉都是反方向的,相信熟悉一段时间之后就好了,本篇的一个简单搭建,希望对你有帮助。

原文链接:https://www.cnblogs.com/huangxincheng/p/17368881.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号