2

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

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

我可以在 64 位 ubuntu 12.10 下很好地使用原生 gcc 和 nasm。

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

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

在此处输入图像描述

我只是一个新手,我知道我可以在 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;
}

世界.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     
4

1 回答 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

When linked together with the C wrapper in the question and run in plain cmd window it works fine:

screenshot

于 2013-01-21T15:07:48.633 回答