简体   繁体   English

将C与内联汇编程序初学者问题一起使用

[英]Using C with inline assembler beginner problem

I am just testing and trying to learn how assembler works with C. So i was browsing around some tutorials and i found this: 我只是在测试并试图了解汇编程序如何与C配合使用。所以我在浏览一些教程时发现:

__asm
{
    mov     ax,0B800h       //startaddress for the screen memory (in textmode)
    mov     es,ax           //add the startaddress to es

    xor     di,di           //reset di (start at the beginning of the screen)

    mov     al, 65          //65 = ascii for the 'A' character to al
    mov     ah, 16*4+1      //Attribute = blue text on a red background to ah.
    mov     cx,2000         //25*80 = 2000 characters on the screen
    rep     stosw           //write ax to the screen memory and count di up 2000 times

} }

The problem i have is that i can't run it, i can compile it inside my main method in Microsoft Visual Studio 2008 but when i run it, it produces this error: 我的问题是我无法运行它,可以在Microsoft Visual Studio 2008的主要方法中编译它,但是当我运行它时,它会产生此错误:

Unhandled exception at 0x00da3660 in Test.exe: 0xC0000005: Access violation reading location 0xffffffff. Test.exe中0x00da3660处未处理的异常:0xC0000005:访问冲突读取位置0xffffffff。

on the second line, mov es,ax //lägg startadressen i es 在第二行上,mov es,ax // //开始播放

Could it be that the program is 16-bit and that VS 2008 compiles it into a 32-bit program? 可能是该程序是16位的,而VS 2008将其编译为32位程序吗? If so, can you force VS 2008 to compile it differently? 如果是这样,您可以强制VS 2008进行不同的编译吗?

Does anyone know of a good inside assembler tutorial ? 有人知道好的汇编程序内部教程吗?

It is 16 bit DOS code assuming a lot of things which aren't true anymore for a long time. 它是16位DOS代码,假定很多事情在很长一段时间不再是真的。 You should better search for some other tutorial. 您最好搜索其他教程。

Hello I found a very good tutorial!, it explains with simple diagrams every detail. 您好,我找到了一个很好的教程!,它用简单的图表说明了每个细节。

It's exactly what you are looking for :)! 这正是您要寻找的:)!

http://rodrigosavage.blogspot.com/2010/07/hello-world-with-inline-asm.html http://rodrigosavage.blogspot.com/2010/07/hello-world-with-inline-asm.html

I rewrite your code as: 我将您的代码重写为:

[BITS 16]
[ORG 7C00h]
main:

mov     ax,0B800h
mov     es,ax
xor     di,di
mov     al, 65
mov     ah, 16*4+1
mov     cx,2000
rep     stosw

times 510-($-$$) db 0
dw 0xAA55

Then save it as xxx.asm, and compile it using "nasm xxx.asm", and subsequently run this inside kvm: "kvm xxx" or you can also "dd" to your harddisk, and boot up directly from the the code and see it running. 然后将其另存为xxx.asm,并使用“ nasm xxx.asm”进行编译,然后在kvm中运行该程序:“ kvm xxx”,或者也可以将其“ dd”安装到硬盘上,并直接从代码和看到它运行。 All done inside Ubuntu environment. 全部在Ubuntu环境中完成。 There are many more similar examples to above here: 这里还有更多与上述类似的示例:

Gavin's Guide to 80x86 Assembly - Part 7: 加文的80x86汇编指南-第7部分:

http://stuff.pypt.lt/ggt80x86a/asm8.htm http://stuff.pypt.lt/ggt80x86a/asm8.htm

rep stosw repeats storing a word from ax to es:di, and your es:di is B800:0 which is arbitrary in protected mode and it may not be mapped in your program, so it gives a segmentation fault. rep stosw重复存储从ax到es:di的单词,并且您的es:di是B800:0,在保护模式下是任意的,并且可能未映射到程序中,因此会产生分段错误。 It looks like an ancient code. 它看起来像一个古老的代码。 If you have DOS, it might just work 如果您使用DOS,则可能会正常工作

Windows does not allow direct access to video memory. Windows不允许直接访问视频内存。 If you want to work in console, you should use console related API . 如果要在控制台中工作,则应使用与控制台相关的API

This is DOS code. 这是DOS代码。 For learning Win32 assembly, the 'classics' are Iczelion's tutorials. 对于学习Win32汇编,“经典”是Iczelion的教程。 Have a look here 在这里看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM