3

我有以下汇编代码(NASM)

global _main

extern _sizeof
extern _printf
extern _objc_msgSend
extern _objc_getClass
extern _sel_registerName

SECTION .data

alert_text : db "NSAlert", 0
alloc_sel  : db "alloc", 0
init_sel   : dw "init", 0

alert_class: resb 4

SECTION .text
_main:

; get the class
sub esp, 8
push alert_text
call _objc_getClass
add esp, 12

; save class
mov dword [alert_class], eax

; get alloc selector
sub esp, 8
push alloc_sel
call _sel_registerName
add esp, 12

; allocate it
sub esp, 4
push alloc_sel
; push alert class
mov eax, dword [alert_class]
push eax

call _objc_msgSend
add esp, 12

mov eax, 0
ret

然后我把它组装起来

$ nasm -fmacho UI.asm -o UI.o
UI.asm:83: warning: uninitialised space declared in __data section: zeroing
$ ld UI.o -macosx_version_min 10.8 -lSystem -framework CoreFoundation -framework AppKit -o UI

当我运行它时,我得到

$ ./UI 
2013-09-28 22:29:31.110 UI[60688:707] *** NSForwarding: warning: selector (0x201c) for message 'alloc' does not match selector known to Objective C runtime (0x97ee8e93)-- abort
2013-09-28 22:29:31.112 UI[60688:707] +[NSAlert alloc]: unrecognized selector sent to class 0xacb12514
2013-09-28 22:29:31.112 UI[60688:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSAlert alloc]: unrecognized selector sent to class 0xacb12514'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x99b28e8b __raiseError + 219
    1   libobjc.A.dylib                     0x9617e52e objc_exception_throw + 230
    2   CoreFoundation                      0x99b2c8ed +[NSObject(NSObject) doesNotRecognizeSelector:] + 253
    3   CoreFoundation                      0x99a74e87 ___forwarding___ + 487
    4   CoreFoundation                      0x99a74c32 _CF_forwarding_prep_0 + 50
    5   UI                                  0x00001ff1 main + 71
)
Trace/BPT trap: 5

我不知道为什么它不起作用,这与保存警报类有关吗?

4

1 回答 1

5

据我所知(诚然,我不是汇编专家),您正在注册选择器,但从不使用它。

sel_registerName声明如下:

SEL sel_registerName(const char *str);

在幕后,c-string 被保留,并返回一个新指针(来自选择器的哈希表),然后必须将其用于未来的方法调用,如下所示:

SEL alloc_sel = sel_registerName("alloc");
id newObject = objc_msgSend(target_class, alloc_sel);

请参阅为什么我们不能将 C 字符串用作 SEL?至于这个问题的原因。

于 2013-09-29T02:45:02.203 回答