views:

485

answers:

5

Hello!

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 ?

+4  A: 

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.

AProgrammer
Does anyone know of a good inside assembler tutorial ?
Patrick
It's a long time since I learned it. If I remember correctly, The Art of Assembly Language of Randall Hyde wasn't bad, even if that author had it pet peeves. http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/index.html seems his current location on the web
AProgrammer
A: 

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

leiz
A: 

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

Kirill V. Lyadvinsky
A: 

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

PhiS
+1  A: 

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

Savashito