【问题标题】:nasm/ld failing to do %include on cygwinnasm/ld 未能在 cygwin 上执行 %include
【发布时间】:2014-12-06 08:14:37
【问题描述】:

我正在尝试在 Cygwin 32 位上使用 nasm 进行汇编(遵循 Dandamudi 的第四部分,Sivarama P.Linux 中的汇编语言编程指南。纽约:Springer,2005)。 p>

但是,我收到以下错误:

$ nasm -f win32 sample.asm 
$ nasm -f win32 io.mac
$ ld -m i386pe io.obj -lc sample.obj -o sample.exe
sample.obj:sample.asm:(.text+0x7): undefined reference to `proc_PutStr'
sample.obj:sample.asm:(.text+0x19): undefined reference to `proc_GetStr'
sample.obj:sample.asm:(.text+0x26): undefined reference to `proc_PutStr'
sample.obj:sample.asm:(.text+0x2e): undefined reference to `proc_GetInt'
sample.obj:sample.asm:(.text+0x3e): undefined reference to `proc_PutStr'
sample.obj:sample.asm:(.text+0x49): undefined reference to `proc_PutInt'
sample.obj:sample.asm:(.text+0x56): undefined reference to `proc_PutStr'
sample.obj:sample.asm:(.text+0x63): undefined reference to `proc_GetCh'
sample.obj:sample.asm:(.text+0x80): undefined reference to `proc_PutStr'
sample.obj:sample.asm:(.text+0x8c): undefined reference to `proc_PutStr'
sample.obj:sample.asm:(.text+0x92): undefined reference to `proc_nwln'
$ 

现在,io.mac 引用了外部过程(例如,proc_GetStr 和 proc_GetCh,我假设它们来自外部库),但我找不到如何引用这些外部过程。

谁能建议我该怎么做?


以下是各种代码/详细说明:

$ cat sample.asm
;An example assembly language program               SAMPLE.ASM
;
;         Objective: To demonstrate the use of some I/O
;                    routines and to show the structure
;                    of assembly language programs.
;            Inputs: As prompted.
;           Outputs: As per input.
%include  "io.mac"

.DATA
name_msg      db   'Please enter your name: ',0
query_msg     db   'How many times to repeat welcome message? ',0
confirm_msg1  db   'Repeat welcome message ',0
confirm_msg2  db   ' times? (y/n) ',0 
welcome_msg   db   'Welcome to Assembly Language Programming ',0

.UDATA
user_name    resb  16             ; buffer for user name
response     resb  1

.CODE
     .STARTUP
     PutStr  name_msg            ; prompt user for his/her name
     GetStr  user_name,16        ; read name (max. 15 characters)
ask_count:
     PutStr  query_msg           ; prompt for repeat count
     GetInt  CX                  ; read repeat count
     PutStr  confirm_msg1        ; confirm repeat count
     PutInt  CX                  ; by displaying its value
     PutStr  confirm_msg2
     GetCh   [response]          ; read user response
     cmp     byte [response],'y' ; if 'y', display welcome message
     jne     ask_count           ; otherwise, request repeat count
display_msg:
     PutStr  welcome_msg         ; display welcome message
     PutStr  user_name           ; display the user name
     nwln
     loop    display_msg         ; repeat count times
     .EXIT   



$ cat io.mac
extern   proc_nwln, proc_PutCh, proc_PutStr
extern   proc_GetStr, proc_GetCh
extern   proc_PutInt, proc_GetInt
extern   proc_PutLInt, proc_GetLInt

;;-------------------------------------------------------------------
%macro  .STARTUP  0
;group dgroup .data .bss
        global   _start
_start:
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  .EXIT  0
        mov    EAX,1
        xor    EBX,EBX
        int    0x80
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  .DATA 0
        segment .data
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro  .UDATA 0
        segment .bss
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro  .CODE 0
        segment .data
        segment .bss
        segment .text
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  nwln  0 
        call    proc_nwln
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  PutCh  1 
        push    AX
        mov     AL,%1
        call    proc_PutCh
        pop     AX
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  PutStr  1 
        push    ECX
        mov     ECX,%1
        call    proc_PutStr
        pop     ECX
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  GetStr  1-2 81
        push    ESI
        push    EDI
        mov     EDI,%1
        mov     ESI,%2
        call    proc_GetStr
        pop     EDI
        pop     ESI
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  GetCh   1
        push    SI
        xor     SI,SI
%ifidni %1,AL
        ;inc     SI
        call    proc_GetCh
%elifidni %1,AH
        mov     SI,1
        call    proc_GetCh
%else
        push    AX
        call    proc_GetCh
        mov     %1,AL
        pop     AX
%endif
        pop     SI
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  PutInt  1
        push    AX
        mov     AX,%1
        call    proc_PutInt
        pop     AX
%endmacro
;;-------------------------------------------------------------------


;;-------------------------------------------------------------------
%macro  GetInt  1
%ifnidni %1,AX
        push    AX
        call    proc_GetInt
        mov     %1,AX
          pop     AX
%else 
        call  proc_GetInt
%endif
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro  PutLInt  1
        push    EAX
        mov     EAX,%1
        call    proc_PutLInt
        pop     EAX
%endmacro
;;-------------------------------------------------------------------

;;-------------------------------------------------------------------
%macro  GetLInt  1
%ifnidni %1,EAX
        push    EAX
        call    proc_GetLInt
        mov     %1,EAX
          pop     EAX
%else 
        call  proc_GetLInt
%endif
%endmacro
;;-------------------------------------------------------------------

$ objdump -t io.obj

io.obj:     file format pe-i386

SYMBOL TABLE:
[  0](sec -2)(fl 0x00)(ty   0)(scl 103) (nx 1) 0x00000000 io.mac
File 
[  2](sec -1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000000 .absolut
[  3](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_nwln
[  4](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutCh
[  5](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutStr
[  6](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetStr
[  7](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetCh
[  8](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutInt
[  9](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetInt
[ 10](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutLInt
[ 11](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetLInt
[ 12](sec -1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000001 @feat.00

$ objdump -t sample.obj 

sample.obj:     file format pe-i386

SYMBOL TABLE:
[  0](sec -2)(fl 0x00)(ty   0)(scl 103) (nx 1) 0x00000000 sample.asm
File 
[  2](sec  1)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x00000000 .data
AUX scnlen 0x95 nreloc 0 nlnno 0
[  4](sec  2)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x00000000 .bss
AUX scnlen 0x11 nreloc 0 nlnno 0
[  6](sec  3)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x00000000 .text
AUX scnlen 0xa1 nreloc 20 nlnno 0
[  8](sec -1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000000 .absolut
[  9](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_nwln
[ 10](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutCh
[ 11](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutStr
[ 12](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetStr
[ 13](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetCh
[ 14](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutInt
[ 15](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetInt
[ 16](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_PutLInt
[ 17](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 proc_GetLInt
[ 18](sec  1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000000 name_msg
[ 19](sec  1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000019 query_msg
[ 20](sec  1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000044 confirm_msg1
[ 21](sec  1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x0000005c confirm_msg2
[ 22](sec  1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x0000006b welcome_msg
[ 23](sec  2)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000000 user_name
[ 24](sec  2)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000010 response
[ 25](sec  3)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 _start
[ 26](sec  3)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x0000001f ask_count
[ 27](sec  3)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000079 display_msg
[ 28](sec -1)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x00000001 @feat.00

$ 

【问题讨论】:

    标签: assembly macros include cygwin nasm


    【解决方案1】:

    你的错误是组装io.mac。它是一个文本文件,包含在sample.asm“原样”中。 io.obj不是组合的io.mac。通过组装io.mac,你破坏了原来的io.obj。但即使你从win_nasm_progs.zip 中提取出原始的io.obj,你也会失败。它不是一个 Windows 文件,而是一个 MS-DOS 文件(是的,这很烦人)。您也不能使用 Linux 文件 (io.o),因为 Cygwin 需要 Windows 系统调用。所以你不能在 Linux 之外使用 Dandamudi 书中的示例。我唯一想到的是将 io.mac 中的调用更改为对 C 函数的调用(和其他调整)并将目标文件与 GCC 链接。

    【讨论】:

    • 我决定放弃 Linux 中汇编语言编程指南 中的 I/O 方案,并采用 Carter, Paul A. PC assembly language. Raleigh, NC: Lulu Press, 2007 的那个(适用于 Cygwin/nasm)跨度>
    【解决方案2】:

    我决定放弃 Linux 中汇编语言编程指南 中的 I/O 方案,并采用 Carter, Paul A. PC assembly language. Raleigh, NC: Lulu Press, 2007 的那个(适用于 Cygwin/nasm) (即它的driver.c cdecl.h asm_io.inc asm_io.asm 文件)

    【讨论】:

      猜你喜欢
      • 2012-12-22
      • 1970-01-01
      • 2022-11-23
      • 2020-12-09
      • 2015-01-23
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多