- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我花了整整一天的时间试图编译一些简单的程序,但到目前为止运气不佳。我要做的是编译和运行以nasm汇编语言编写的程序。
我已升级到最新的nasm(v2.10.09)。因此,让我跳入代码,因为我对这些事情还不了解。这是在linux上使用elf
格式和链接的gcc gcc运行的汇编代码的一部分(评论是我对正在发生的事情的理解):
bits 32
extern printf
global main
section .data
message db "Hello world!", 10, 0
section .text
main:
pushad ;push all registers on stack
push dword message ;push the string on stack
call printf ;call printf
add esp, 4 ;clear stack
popad ;pop all registers back
ret ;return to whoever called me
printf
和
scanf
?
scanf
和
printf
值返回的另一种徒劳尝试(实际上是编译和链接-甚至运行!):
[bits 32] ; why the []?
section .data
input_string db "Enter limit: %d", 0
output_string db "Value %d", 10, 0
counter dd 10
limit dd 0
;nasm -f macho -o test.o test.asm
;ld -lc -o test -arch i386 test.o -macosx_version_min 10.7
section .text
global start
extern _printf
extern _scanf
extern _exit
start:
push ebp ;push stack base
mov ebp, esp ;base is now current top
and esp, 0xFFFFFFF0 ;align the stack - WHY? I just googled this?
sub esp, 16 ;16 bytes for variables - 16 to keep the stack "aligned". WHY?
mov dword[esp], input_string ;ordinary push just isint good nuff for mac... WHY?
mov dword[esp + 4], limit
call _scanf ;does scan something but doesnt print the message, it just scans and then prints the message
mov eax, [limit] ;since i cant push this lets put it in eax first
mov dword[esp + 8], output_string ;push the output string. WHY again MOV?
mov dword[esp + 12], eax ;and the previusly "scanned" variable
call _printf ;print it out
mov dword[esp], 0 ;return value
call _exit ;return
nasm -f macho -o test.o test.asm
进行编译,并将其与
d -lc -o test -arch i386 test.o -macosx_version_min 10.7
链接。无法正常工作。在linux上,此scanf和printf东西 super 容易。怎么了能做得更简单吗?
nasm -f macho -o out.o test.asm
进行编译,但是不使用
gcc -o test out.o
或通过使用
ld -lc -o test -arch i386 out.o -macosx_version_min 10.7
进行链接,并附加平面
-arch i386
也不能解决该问题。如果可以编写“类似于Linux的程序集”,我将非常高兴,因为我不必担心堆栈对齐和类似的问题。
ld: warning: ignoring file out.o, file was built for i386 which is not the architecture being linked (x86_64): out.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
Undefined symbols for architecture i386:
"printf", referenced from:
main in out.o
"start", referenced from:
-u command line option
ld: symbol(s) not found for architecture i386
最佳答案
您在询问有关代码的很多问题,而您实际上根本不了解其中的汇编代码。
首先,由于您编写代码的方式,main
例程将成为C风格程序的入口。由于mac os x链接的工作方式;您必须将其命名为_main
,以匹配链接程序在生成可执行文件时引入/usr/lib/crt1.o
时作为默认程序入口点的链接程序所寻找符号的名称(如果对文件进行nm处理,则将看到类似U _main
的条目。类似地,所有库例程都以前导下划线开头,因此如果要使用它们,则必须使用该前缀。
其次,MAC OS调用约定要求所有call
的堆栈的16字节对齐,这意味着您必须确保堆栈指针在每个点都进行了相关对齐。在主例程的入口点,您已经知道由于将返回地址存储在堆栈中以便从main返回而错位了。这意味着,即使您要进行单个调用,也必须将堆栈向下移动至少12个字节才能进行调用。
有了这些信息,我们将省略ebp
,只为代码目的而专门使用esp。
这是假设的序言:
bits 32
extern _printf
global _main
section .data
message db "Hello world!", 10, 0
section .text
_main:
_main
时,重新调整堆栈:
sub esp, 12
mov dword[esp], message
call _printf
add esp, 12
main
的返回码,并返回:
mov eax, 0
ret
eax
作为例程的返回码,只要它适合寄存器即可。编译并链接代码后:
nasm -f macho -o test.o test.asm
ld -o test -arch i386 test.o -macosx_version_min 10.7 -lc /usr/lib/crt1.o
0
退出。
scan_string db "%d", 0
limit dd 0
scan_string
的地址存储在
esp
中,然后将limit的地址存储在
esp + 4
中,然后调用scanf:
mov dword[esp], scan_string
mov dword[esp + 4], limit
call _scanf
output_string db "Value %d", 10, 0
mov dword[esp], output_string
esp + 4
-即printf的第二个参数:
mov eax, [limit]
mov dword[esp + 4], eax
call _printf
_exit
函数-这与简单的print变体不同,因为我们实际上是在调用exit,而不是简单地返回。
mov dword[esp], 0
call _exit
Because that's how Mac OS X does it
It is, but we aligned the stack at the start of the routine and an aligned stack is a functioning stack, by pushing and popping you're messing with the alignment. This is one of the purposes behind using the
ebp
register rather than theesp
register.
ebp
寄存器,则函数prolog如下所示:
push ebp
mov ebp, esp
sub esp, 8 ; in this case to obtain alignment
add esp, 8
pop ebp
关于c - Mac OS X使用main和scanf/printf的32位nasm汇编程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20082414/
更新到 Xcode 12.2 后,由于与 Apple Silicon 相关的链接错误,我的项目开始无法编译。我似乎已经修复了大部分问题,但是一个构建静态链接框架的子项目给我带来了问题。然而,具有明显相
我有一台旧的 MacBook Pro,我在其中制作了两个应用程序并提交到应用程序商店。所以基本上签名身份在该机器的钥匙串(keychain)中。在 Mavericks 升级后,我不得不从那台计算机转移
我正在 MAC OSX 10.6 上编写一个示例应用程序,其 gcc 版本为 4.2。我正在使用 gcc 4.2 版编译应用程序。它在同一台机器上工作正常,但在 MAC OSX 10.5 (gcc 4
这是我的简单 mac 地址生成器: private String randomMACAddress(){ Random rand = new Random(); byte[] macA
我一直在寻找一种将十进制 MAC 地址转换为十六进制地址的方法。 例如 170.187.204.0.17.34至AA:BB:CC:00:11:22 . 致Convert HEX to Decimal
我想使用 UISceneSession 的委托(delegate)方法当用户将注意力从应用程序(窗口)移开,然后又回到应用程序(窗口)时,生命周期有助于通知我的 Mac Catalyst 应用程序。
我在签署 Mac 应用程序安装程序时遇到问题,我计划在 Mac 应用商店之外分发该应用程序。我正在使用开发人员安装程序证书来签署应用程序,但它给出了一些错误。下面是我用来签署应用程序的命令。 prod
Mac Catalyst 允许调整窗口大小,有没有办法为 Mac Catalyst 应用程序提供最小窗口大小? 最佳答案 只需将以下代码块添加到您的 application:didFinishLaun
这是一个非常理论性的问题,但对我来说很安静,即我如何进行下一步。 我正在开发一个SwiftUI MacOS应用程序,用户可以在其中上传自己的文件。元数据将存储在CoreData中,而我将文件手动存储在
滑动删除在 maccatalyst 中不起作用。相同的代码在 iPad 上运行良好。 在 maccatalyst 中未调用 UITableview trailingSwipeActionsConfig
我有两台 Mac,在进行 iPad 开发时,如果可以让另一台 Mac 启动模拟器并在构建完成后加载应用程序,我很感兴趣。 如果 iPad 应用程序在一台 Mac 屏幕和 Xcode 的模拟器中运行,所
我有一个用 objective-c 开发的 mac 应用程序。cpp 中还有另一个命令行中间应用程序,它是 native 主机应用程序,用于接收来自 chrome 扩展的消息。每当中间应用程序从扩展程
是否可以使用来自 Comodo 或 Thawte 的代码签名证书来签署应用程序并通过 Gatekeeper,或者我需要为此目的拥有 Mac 开发者订阅? 最佳答案 您必须是 Mac Developer
我正在使用 C++ 和 OpenGL/SDL 编写一个游戏,使用 Visual Studio 作为我的 IDE。我没有 Mac,甚至对这个平台都不熟悉。但我还是想发布给 Mac 用户。 我有三个问题。
我想将 MAC 地址 00163e2fbab7(存储为字符串)转换为其字符串表示形式 00:16:3e:2f:ba:b7。最简单的方法是什么? 最佳答案 使用一种完全迂回的方法来利用现有的一次将两个十
无法连接到Mac上的MySQL工作台。我收到以下错误:无法连接,服务器可能未运行。无法连接到‘127.0.0.1’上的MySQL服务器(61)如有帮助,将不胜感激。。谢谢!
我已经搜索了很长时间,似乎无法找到这个问题的答案。在 SO 上只找到两个问题/答案,但他们仍然没有回答这个问题 ( https://stackoverflow.com/search?q=netcore
我们在 Docker for Mac 中有一个 LoadBalancer 真是太酷了。 我对创建的端口有疑问: apiVersion: v1 kind: Service metadata: nam
我有一个我一直在从事的小型开源 OSX 项目,我想在 App Store 之外分发。 随着即将发布的 Mountain Lion,我想提供一个证书,以减少安装过程中的痛苦。 使用 App Store,
我的一台 Mac 没有互联网连接。我需要使用 docker pull。我的想法是,我将在我的一台具有互联网连接的 Mac 中使用 docker pull,然后将其复制到我没有互联网连接的 Mac。如何
我是一名优秀的程序员,十分优秀!