首页 文章

在Cygwin shell上运行链接的C对象文件(从C和x86-64程序集编译),没有输出?

提问于
浏览
2

试图处理我的汇编语言任务......

有两个文件,hello.c和world.asm,教授要求我们使用gcc和nasm编译这两个文件并将目标代码链接在一起 .

我可以在64位ubuntu 12.10下完成,使用本机gcc和nasm .

但是当我通过cygwin 1.7在64位Win8上尝试相同的事情时(首先我尝试使用gcc但不知何故-m64选项不起作用,并且因为教授要求我们生成64位代码,我用Google搜索并找到一个名为mingw-w64的软件包,它有一个编译器x86_64-w64-mingw32-gcc,我可以使用-m64),我可以将文件编译为mainhello.o和world.o并将它们链接到main.out文件,但不知何故,当我输入“./main.out”并等待“Hello world”时,没有任何反应,没有输出没有错误消息 .

新用户因此无法发布图片,抱歉,这是Cygwin shell中发生的事情的屏幕截图:

enter image description here

我只是一个新手,我知道我可以在ubuntu下完成任务,但我只是好奇这里发生了什么?

感谢你们

你好ç

//Purpose:  Demonstrate outputting integer data using the format specifiers of C.
//
//Compile this source file:     gcc -c -Wall -m64 -o mainhello.o     hello.c
//Link this object file with all other object files:  
//gcc -m64 -o main.out mainhello.o world.o
//Execute in 64-bit protected mode:  ./main.out
//

#include <stdio.h>
#include <stdint.h> //For C99 compatability

extern unsigned long int sayhello();

int main(int argc, char* argv[])
{unsigned long int result = -999;
 printf("%s\n\n","The main C program will now call the X86-64 subprogram.");
 result = sayhello();
 printf("%s\n","The subprogram has returned control to main.");
 printf("%s%lu\n","The return code is ",result);
 printf("%s\n","Bye");
 return result;
}

world.asm

;Purpose:  Output the famous Hello World message.


;Assemble: nasm -f elf64 -l world.lis -o world.o world.asm


;===== Begin code area 

extern printf    ;This function will be linked into the executable by the linker

global sayhello

segment .data    ;Place initialized data in this segment

          welcome db "Hello World", 10, 0
          specifierforstringdata db "%s", 10,


segment .bss    

segment .text   

sayhello:        

;Output the famous message
mov qword rax, 0       
mov       rdi, specifierforstringdata
mov       rsi, welcome
call      printf

;Prepare to exit from this function
mov qword rax, 0                                  
ret;                                              

;===== End of function sayhello

1 回答

  • 0
    ;Purpose:  Output the famous Hello World message.
    ;Assemble: nasm -f win64 -o world.o world.asm
    
    ;===== Begin code area
    
    extern _printf    ;This function will be linked into the executable by the linker
    global _sayhello
    
    segment .data    ;Place initialized data in this segment
              welcome db "Hello World", 0
              specifierforstringdata db "%s", 10, 0
    
    segment .text
    
    _sayhello:
    ;Output the famous message
    sub       rsp, 32 ; shadow space
    mov       rcx, specifierforstringdata
    mov       rdx, welcome
    call      _printf
    add       rsp, 32 ; clean up stack
    
    ;Prepare to exit from this function
    mov qword rax, 0
    ret
    
    ;===== End of function sayhello
    

    当与问题中的C包装器链接在一起并在普通的cmd窗口中运行时,它可以正常工作:

    screenshot

相关问题