10.Ubuntu下开发STM32--再谈exit

16 篇文章 1 订阅

在笔记6—使用串口Part2(https://www.stmcu.org.cn/module/forum/thread-603909-1-1.html)中,我们简单的介绍了一下exit。在windows或者linux操作系统中,main的返回值是有意义的,类型为int。这个返回值实际上是返回给操作系统的。0表示程序正常终止,其他非零表示程序异常终止。

在使用ST固件库提供的启动文件和syscalls.c文件之后,如果程序中主动调用exit,能跳转到_exit系统调用中。如果main中使用return,并不能跳到_exit系统调用中。这次我们通过更改启动文件,来将main的return传递给_exit.

函数参数和返回值的传递

根据ARM C/C++过程调用标准,如果函数的参数少于或者等于4个,那么第一个参数使用R0传递,第二个使用R1,依次类推。如果多于4个参数,那么前四个使用R0-R3传递,后面的参数使用栈传递。因为函数最多只能有1个返回值,返回值仅使用R0进行传递。

考虑这么一种情况,当main函数执行完之后,返回值保存在R0中。而void _exit(int status)期望1个参数,这个参数正好是main的返回值,因此在汇编中可以这样调用:

img

这样就巧妙的将main的返回值传递给_exit进行处理。看到这样的启动代码,想必你也就不再惊讶我的串口的初始化代码在哪了。(可以去platform/init.c中找找看)。

下面写点代码进行测试。

测试返回值

img

int __io_puts(const char *str);是在usart.c文件中实现的函数,可以将一个字符串通过串口发送出去,返回实际发送的个数。sys_itoa可以将一个int型数字转换成字符串,返回的是字符串的地址。这个函数是从Apple open source中复制的。只不过改了个名字而已。详见地址:http://www.opensource.apple.com/source/groff/groff-12/groff/libgroff/itoa.c 。

img

main的代码:

img

测试结果截图:

img

本次相关的代码在exit.zip中。

W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy InRelease' is not signed. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://security.ubuntu.com/ubuntu jammy-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://security.ubuntu.com/ubuntu jammy-security InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-updates InRelease' is not signed. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease: The key(s) in the keyring /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg are ignored as the file is not readable by user '_apt' executing apt-key. W: GPG error: http://archive.ubuntu.com/ubuntu jammy-backports InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 871920D1991BC93C E: The repository 'http://archive.ubuntu.com/ubuntu jammy-backports InRelease' is not signed. E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true' E: Sub-process returned an error code
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值