• C

More Assembly

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.