More Assembly

Posted on 1997-05-09
Last Modified: 2010-04-15
Working an external assembly function that will be called from C as AsmPixel(int x, int y, int value) will put a value into the y*320+x location of unsigned char far *double_buffer this is the assembly code that I have so far that crashes when ran.

Parms struc
         dw    2 dup(?)
x        dw    ?
y        dw    ?
value    dw    ?
Parms ends

    .model   small
    public _AsmPixel

extrn _double_buffer:word

_AsmPixel    proc    near

      mov    ax,_double_buffer
      mov    es,ax
      push   bp
      mov    dx, 320   ; Put 320 in DX
      mul    dx        ; multiply AX (Y) by DX (320)
      add    ax, bx    ; add BX (X) to AX -> 320*y+x
      add    di, ax    ; add to pointer offset ***
;ES:DI now points at &double_buffer[320*y+x]
      mov    es:[di], cl ; Store the byte

_AsmPixel      endp

When a simple c program is ran with this function it crashes. Could someone help me with what is wrong.
Question by:silverstream
  • 2

Expert Comment

ID: 1250316
Did you forgot type pop bp at the end of the function?
And of course , push and pop es,di and every registeres you are using.

Another thing, try to move es at the end of the buffer, not at the begining.


Accepted Solution

klos earned 70 total points
ID: 1250317
You need to set up BP after pushing it, preserve DI (and maybe ES depending on compiler), pop BP, and don't forget the return statement!  ("Endp" doesn't do a "Ret"!)  There are several other  problems as well.

Try the following:

Parms   struc
        dw      2 dup(?)
x       dw      ?
y       dw      ?
value   dw      ?
Parms   ends

        .model small

        public  _AsmPixel

        extrn   _double_buffer:word

_AsmPixel proc near
        Push    BP
        Mov     BP,SP
        Push    DI
        Push    ES

        mov     ax,_double_buffer
        mov     es,ax

        Mov     AX,[BP].y       ;Load Y into AX
        mov     dx, 320         ; Put 320 in DX
        mul     dx              ; multiply AX (Y) by DX (320)
        add     ax,[BP].x       ; add X to AX -> 320*y+x
        Mov     DI,AX           ;Copy offset to DI
;ES:DI now points at &double_buffer[320*y+x] ***
;[well, not really.  ES:DI points to _double_buffer:[y*320+x]
;where _double_buffer is a word containing the base segment
;address of the array in question]

        Mov     CL,Byte Ptr [BP].value                    
        mov     es:[di], cl ; Store the byte

        Pop     ES
        Pop     DI
        Pop     BP
_AsmPixel endp

Expert Comment

ID: 1250318
More info:  Since _double_buffer is a char far *, ES AND DI should be loaded as follows:

        Les     DI,Dword Ptr _double_buffer

And rather than loading DI with AX after the multiplication, you should ADD AX to DI (which you originally had, but was confusing since you hadn't loaded DI prior to that!).

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 95
Where is my core dump file in Ubuntu? 12 439
undefined reference to `bswap_128' 9 136
chcp 65001 File encoding 66 230
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now