Solved

More Assembly

Posted on 1997-05-09
3
305 Views
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
    .486p
    .code
    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
      end

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

Expert Comment

by:smediodia
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.


0
 

Accepted Solution

by:
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
        .486p
        .code

        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
        Ret
_AsmPixel endp
        end
0
 

Expert Comment

by:klos
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!).
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 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

22 Experts available now in Live!

Get 1:1 Help Now