Question

I am just testing and trying to learn how assembler works with C. So i was browsing around some tutorials and i found this:

__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:

Unhandled exception at 0x00da3660 in Test.exe: 0xC0000005: Access violation reading location 0xffffffff.

on the second line, mov es,ax //lägg startadressen i es

Could it be that the program is 16-bit and that VS 2008 compiles it into a 32-bit program? If so, can you force VS 2008 to compile it differently?

Does anyone know of a good inside assembler tutorial ?

Was it helpful?

Solution

It is 16 bit DOS code assuming a lot of things which aren't true anymore for a long time. You should better search for some other tutorial.

OTHER TIPS

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

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. All done inside Ubuntu environment. There are many more similar examples to above here:

Gavin's Guide to 80x86 Assembly - Part 7:

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. It looks like an ancient code. If you have DOS, it might just work

Windows does not allow direct access to video memory. If you want to work in console, you should use console related API.

This is DOS code. For learning Win32 assembly, the 'classics' are Iczelion's tutorials. Have a look here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top