Cygwin:汇编语言开发?

发布于 2024-07-13 18:42:09 字数 975 浏览 4 评论 0原文

首先,我不确定这是否应该成为我的线程的一部分 昨天开始程序集和堆栈,但我认为 我在这里问的问题完全不同。

我一直在尝试了解 Cygwin 到底是什么,通过 维基百科和谷歌,我运气不太好。 我刚刚开始使用 gcc 在 Linux 上进行汇编编程 气体装配工。 我在午餐时在工作时使用一台机器 上面只有Windows。 我想练习一些组装 这里的语言编程所以我认为 Cygwin 可能能够 帮助。

我错误地相信我在 Linux 中编写的代码 可以使用 Cygwin 在 Windows 中编译和运行。 Cygwin 允许我很好地编译代码:

as someAssmProg.as -o someAssmProg.o
ld someAssmProg.o -o someAssmProg

但是如果我尝试在 Cygwin 下运行代码,

./someAssmProg

我会收到“发生未处理的 win32 异常”消息

现在我假设这是因为我正在编写的代码是 适用于 Linux。 我以为 Cygwin 会处理 有了这个。 Cygwin 真的只是用来开发吗 以 Unix 风格的命令行方式运行 Windows 应用程序?

我再次知道这对于这里的大多数人来说可能是显而易见的,但是 我真的很困惑!

PS 我之前曾在 Windows 上尝试过 AndLinux,但安装量相当大。

Firstly I'm not sure if this should be part of the thread I
started yesterday on assembly and the stack but I think
the question I'm asking here is quite different.

I'm been trying to understand what exactly Cygwin is, via
Wikipedia and Google, I'm not having much luck.
I've just begun assembly programming on Linux using the gcc
gas assembler. I'm using a machine at work during lunch that
only has Windows on it. I wanted to practice some assembly
language programming here so I thought Cygwin might be able
to help.

Mistakenly I believed that the code I was writing in Linux
could just be compiled and run in Windows using Cygwin.
Cygwin allows me to compile the code fine:

as someAssmProg.as -o someAssmProg.o
ld someAssmProg.o -o someAssmProg

But if I try to run the code under Cygwin,

./someAssmProg

I get a "unhandled win32 exception occured" message

Now I am assuming this is because the code I'm writing is
intended for Linux. I thought though that Cygwin would deal
with this. Is Cygwin just really meant to be used to develop
Windows applications in a Unix style command line way?

Again I know this is probably obvious to most folks here but
I'm genuinely confused!

P.S I've tried AndLinux before for Windows, but it's quite a hefty install.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难以启齿的温柔 2024-07-20 18:42:09

Cygwin 是一个运行时层,它在 Windows 上提供来自 Unix/Linux 世界的库和软件。 它不会让你的汇编代码运行,因为底层的操作系统仍然是Windows,所以所有的中断等仍然是Windows版本,而不是Linux版本。 此外,如果您确实想在 Windows 上运行汇编程序,则必须采取与 Linux(或 DOS)中不同的方式进行操作。

Cygwin is a runtime layer that provides libraries and software from the Unix/Linux world on Windows. It will not allow your assembler code to run, as the OS is still Windows underneath, so all the interrupts, etc. are still the Windows, not Linux, versions. In addition, if you really want to run assembler on Windows, you have to do things a lot differently than you do in Linux (or DOS, for that matter).

不醒的梦 2024-07-20 18:42:09

您完全可以在 Cygwin 中运行汇编程序。 我猜你的加载失败了,因为在 Windows 执行进程和进入 main 函数之间必须发生很多事情。 当 gcc 将程序集作为输入时,它将链接到适当的样板代码以生成有效的可执行文件。

这是一个示例汇编程序。 将其另存为 hello.s:

.intel_syntax noprefix

.section .text

.globl _main
_main:
        enter 0, 0

        // welcome message
        push    OFFSET s_HelloWorld
        call    _printf
        pop     eax

        // add three numbers
        push    2
        push    4
        push    5
        call    _addThree
        add     esp, 3 * 4

        // print returned value
        push    eax
        push    OFFSET s_PercentD
        call    _printf
        add     esp, 2 * 4

        xor     eax, eax
        leave
        ret

// Add three numbers
_addThree:
       enter    0, 0
       mov      eax, DWORD PTR [ebp + 8]
       add      eax, DWORD PTR [ebp + 12]
       add      eax, DWORD PTR [ebp + 16]
       leave
       ret

.section .rdata

s_HelloWorld:
       .ascii  "Hello, world.\n\0"
s_PercentD:
       .asciz "%d\n"

然后运行它

$ gcc -mno-cygwin hello.s -o hello && ./hello
Hello, world.
11

处理器汇编指令的参考包含在 AMD64 架构程序员手册。 C 调用约定记录在 此页面来自互联网档案馆; 也许你可以找到一个仍然有图像的类似的?

请注意,Cygwin 目前仅进行 32 位汇编; (非消费者)世界现在都是 64 位,在现代处理器的 64 位模式下,您有更多的寄存器和不同的调用约定。

You can totally run assembly programs in Cygwin. I’m guessing that your load failed because there’s a bunch of stuff that has to happen between when Windows executes a process and when you get to the main function. When gcc is given assembly as input, it will link in the appropriate boilerplate code to generate a valid executable.

Here’s a sample assembly program. Save it as hello.s:

.intel_syntax noprefix

.section .text

.globl _main
_main:
        enter 0, 0

        // welcome message
        push    OFFSET s_HelloWorld
        call    _printf
        pop     eax

        // add three numbers
        push    2
        push    4
        push    5
        call    _addThree
        add     esp, 3 * 4

        // print returned value
        push    eax
        push    OFFSET s_PercentD
        call    _printf
        add     esp, 2 * 4

        xor     eax, eax
        leave
        ret

// Add three numbers
_addThree:
       enter    0, 0
       mov      eax, DWORD PTR [ebp + 8]
       add      eax, DWORD PTR [ebp + 12]
       add      eax, DWORD PTR [ebp + 16]
       leave
       ret

.section .rdata

s_HelloWorld:
       .ascii  "Hello, world.\n\0"
s_PercentD:
       .asciz "%d\n"

then run it with

$ gcc -mno-cygwin hello.s -o hello && ./hello
Hello, world.
11

The reference for your processor’s assembly instructions are contained in the AMD64 Architecture Programmer’s Manual. The C calling convention is documented in this page from the Internet Archive; maybe you can find a similar one that still has the images?

Note that Cygwin will only do 32-bit assembly right now; the (non-consumer) world is all 64 bits now, and in 64-bit mode on modern processors you have many more registers and different calling conventions.

梦萦几度 2024-07-20 18:42:09

最好的方法是安装一个虚拟机(使用 VirtualBox 或类似的东西)并在其下完整安装 Linux。 听起来这可能太重了,无法满足您的要求,但这是我目前唯一的建议。

The best way to do it really is to install a VM (using VirtualBox or somesuch) with a full install of Linux under it. It sounds like that may be too heavy to meet your requirements, but that's the only suggestion I have at the moment.

你是暖光i 2024-07-20 18:42:09

嗨,我遇到了类似的问题,但能够使用 -mconsole 解决它
在此 cygwin 会话中,选项首先编译文件 foo.c,然后使用 gdb 调试器运行我希望这会对您有所帮助

$ cat foo.c
int main( void )
{
        __asm__ ( "mov $5, %rax" );
        return 0;
}
$ make
gcc -ggdb -Wall -mconsole -o foo.o -c foo.c
gcc -ggdb -o foo foo.o -lm
$ gdb ./foo.exe
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./foo.exe...done.
(gdb) l
1       int main( void )
2       {
3               __asm__ ( "mov $5, %rax" );
4               return 0;
5       }
(gdb) b 3
Breakpoint 1 at 0x1004010ed: file foo.c, line 3.
(gdb) r
Starting program: /cygdrive/c/C++/C/foo.exe
[New Thread 6724.0x13d0]
[New Thread 6724.0x1890]
[New Thread 6724.0x8e0]
[New Thread 6724.0xd84]

Breakpoint 1, main () at foo.c:3
3               __asm__ ( "mov $5, %rax" );
(gdb) info registers rax
rax            0x0      0
(gdb) s
4               return 0;
(gdb) info registers rax
rax            0x5      5
(gdb) c
Continuing.
[Thread 6724.0x1890 exited with code 0]
[Thread 6724.0x8e0 exited with code 0]
[Thread 6724.0xd84 exited with code 0]
[New Thread 6724.0x1368]
[Inferior 1 (process 6724) exited normally]
(gdb)

hi i had a similar problem but was able to figure it out using the -mconsole
option in this cygwin session a file foo.c is first compiled and than run with the gdb debugger i hope that will help you

$ cat foo.c
int main( void )
{
        __asm__ ( "mov $5, %rax" );
        return 0;
}
$ make
gcc -ggdb -Wall -mconsole -o foo.o -c foo.c
gcc -ggdb -o foo foo.o -lm
$ gdb ./foo.exe
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./foo.exe...done.
(gdb) l
1       int main( void )
2       {
3               __asm__ ( "mov $5, %rax" );
4               return 0;
5       }
(gdb) b 3
Breakpoint 1 at 0x1004010ed: file foo.c, line 3.
(gdb) r
Starting program: /cygdrive/c/C++/C/foo.exe
[New Thread 6724.0x13d0]
[New Thread 6724.0x1890]
[New Thread 6724.0x8e0]
[New Thread 6724.0xd84]

Breakpoint 1, main () at foo.c:3
3               __asm__ ( "mov $5, %rax" );
(gdb) info registers rax
rax            0x0      0
(gdb) s
4               return 0;
(gdb) info registers rax
rax            0x5      5
(gdb) c
Continuing.
[Thread 6724.0x1890 exited with code 0]
[Thread 6724.0x8e0 exited with code 0]
[Thread 6724.0xd84 exited with code 0]
[New Thread 6724.0x1368]
[Inferior 1 (process 6724) exited normally]
(gdb)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文