0

Please, does anybody know how to code overloading in assembly language? I have error:

(70),(72) A4057: Illegal size for operand

CODE WITH ERROR:

mov   ax, offset ob1.DRAW
mov   _this.DRAW, ax      //(70)
mov   ax, seg ob1.DRAW
mov   _this.DRAW+2, ax    //(72)

I try do program very similar to this(Example 14): http://www.drdobbs.com/embedded-systems/object-oriented-programming-in-assembly/184408319

My code:

    .MODEL SMALL
dseg            segment byte public 'data'
_This           equ     es:[bx]         ;Provide a mnemonic name for THIS.
Shape_Draw PROC FAR
    MOV   AX,@DATA
    MOV   DS,AX
    LEA DX,MSG3         ;print msg3
    MOV AH,09H
    INT 21H
    RET
Shape_Draw ENDP

Rect_Draw PROC FAR
    MOV   AX,@DATA
    MOV   DS,AX
    LEA DX,MSG1         ;print msg1
    MOV AH,09H
    INT 21H
    RET
Rect_Draw ENDP

Circle_Draw PROC FAR
    MOV   AX,@DATA
    MOV   DS,AX
    LEA DX,MSG2         ;print msg2
    MOV AH,09H
    INT 21H
    RET
Circle_Draw ENDP

Shape    struc
ix       dw      ?     ;Wspolrzedna X
iy       dw      ?     ;Wspolrzedna Y
Draw     dd      Shape_Draw     ;Default (overridden) DRAW routine
Shape    ends
;
Rect     struc
         dw      2 dup (?)      ;Reserve space 
         dd      Rect_Draw      ;Draw a rectangle
Rect     ends
;
Circle   struc
         dw      2 dup (?)      ;Reserve space 
         dd      Circle_Draw    ;Draw a circle
Circle   ends

dseg        ends
.DATA
MSG1   DB  "Klasa RECT: ",'$'
MSG2   DB  "Klasa CIRCLE: ",'$'
MSG3   DB  "Klasa SHAPE: ",'$'
ob1     Shape       <'1','1'>
ob11        Shape       <'2','2'>
ob2     Rect        <,>
ob3     Circle      <,>


cseg        segment byte public 'CODE'
        assume  cs:cseg, ds:dseg, es:dseg

main:  

mov      bx, seg ob1
mov      es, bx
mov      bx, offset ob1
call     ob1.Draw          ;Assuming S1 is in the data seg


mov   ax, offset ob1.DRAW
mov   _this.DRAW, ax
mov   ax, seg ob1.DRAW
mov   _this.DRAW+2, ax



MOV AH,4CH 
    INT 21H
END

cseg end

I don't know Assembly Language. :( I insert my code in c++ to explain what I want to do.

    class Figura{
    public:
    int x=2;
    int y=3;
    virtual void draw();
};
class Rect :public Figura{
    public:
    void draw(){
        cout << "RECT DRAW" << endl;
    };
};

int main()
{
    Rect object;
    object.draw();
    return 0;
}
  • 1
    Presumably your assembler doesn't like that you are trying to set a dword in two halves. Look in the manual to see if you can override that or just split the structure field into two. In fact the very document you linked shows how to do it: `mov word ptr _this.DRAW, ax` – Jester Aug 07 '18 at 23:59
  • Polymorphism and overloading are source-level shortcuts that end up calling or inlining different code when the same code is inlined into different places. The asm equivalent is macros. What you're doing is more like **type-punning**, where you access data with different sizes / types than its definition. Other than MASM-style association between labels and sizes, assembly doesn't have types at all; it's all just bytes. – Peter Cordes Aug 08 '18 at 02:06
  • In my previous comment, I was talking about the case where you can resolve the polymorphic type at compile time. It looks like you're implementing virtual functions here like a C++ compiler would, for runtime polymorphism. See [How do objects work in x86 at the assembly level?](https://stackoverflow.com/q/33556511). (But overloading is just a way of using the same function name for multiple types, and doesn't do anything you can't do with C-style `sin(double)` vs. `sinf(float)` vs. `sinl(long double)`. – Peter Cordes Aug 08 '18 at 04:02

0 Answers0