Solved

More Assembly

Posted on 1997-05-09
3
309 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compile VxWorks Toronado project under Visual Studio 11 223
Trouble linking program with -lcrypt 3 155
Problem to save 10 174
Computer slow / BSOD 10 64
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 recursion in the C programming language.

820 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