Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Cygwin shell上运行链接的C对象文件(从C和x86-64程序集编译),没有输出?_C_Assembly_Cygwin_X86 64_Output - Fatal编程技术网

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

在Cygwin shell上运行链接的C对象文件(从C和x86-64程序集编译),没有输出?,c,assembly,cygwin,x86-64,output,C,Assembly,Cygwin,X86 64,Output,正在处理我的汇编语言作业 有两个文件,hello.c和world.asm,教授要求我们使用gcc和nasm编译这两个文件,并将目标代码链接在一起 我可以在64位Ubuntu12.10下使用原生gcc和nasm很好地完成它 但是,当我通过cygwin 1.7在64位Win8上尝试同样的方法时(首先我尝试使用gcc,但不知何故,-m64选项不起作用,而且由于教授要求我们以64位生成代码,我在谷歌上搜索并找到了一个名为mingw-w64的包,它有一个我可以使用-m64的编译器x86_64-w64-mi

正在处理我的汇编语言作业

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

我可以在64位Ubuntu12.10下使用原生gcc和nasm很好地完成它

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

新用户因此无法发布图像,对此表示抱歉,以下是Cygwin shell中发生的情况的屏幕截图:

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

谢谢你们

你好,c

//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;
}
;目的:输出著名的Hello World消息。
;汇编:nasm-f win64-o world.o world.asm
;===== 开始代码区
外部打印;此函数将由链接器链接到可执行文件中
全球(你好)
数据段;将初始化的数据放在此段中
欢迎db“你好,世界”,0
字符串数据数据库“%s”的说明符,10,0
段.文本
_您好:
;输出著名的消息
副rsp,32;阴影空间
mov rcx,字符串数据的说明符
欢迎光临
调用printf
加上rsp,32;清理烟囱
;准备退出此功能
mov-qword-rax,0
ret
;===== 结束函数sayhello
当与问题中的C包装器链接并在普通cmd窗口中运行时,它可以正常工作:


cygwin生成的可执行文件是否在linux系统上运行?检查文件大小和调试信息。
printf()
的调用约定在这两种情况下是否相同?你能用反汇编
main()
来检查这一点吗?您可以使用gcc通过
-S
开关将C代码翻译成汇编代码。我首先在第一个printf之后添加“fflush(stdout)”,这样,您就可以看到是否可以访问sayhello。如果这没有帮助,请尝试使用gdb!Alexey很好地指出了召开会议的重要性。Windows与Linux不同,因此您的参数应该在RCX、RDX、R8和R9中,而不是RSI、RDI等。不确定您是否需要eax中的任何内容。Cygwin有缺陷且不可靠。如果你想做Linux编程,请使用Linux。谢谢大家,@matstpeterson。所以我添加了fflush(stdout),它可以帮助打印出第一行,但其余的没有显示出来。我根据Mats修改了一些代码:
mov-qword-rax,0
mov-qword-rax,0
mov-rcx,stringdata的说明符
mov-rdx,欢迎
调用printf
mov-qword-rax,0
但它不起作用。我在谷歌上做了更多的搜索,发现了一个有类似问题的线程[,这可能表明在windows上这样做是不可能的。。
;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