More Assembly

Posted on 1997-05-09
Medium Priority
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 140 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

621 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