Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 18th, 2007, 7:22 PM   #1
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 15 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
OOP in assembly?

I recently argued on another forum that OOP is not possible in assembly. My post was immediately criticized by others who claimed that it was. (Their arguments, IMO, were less than satisfactory.) Is this true?

How can you write an OO assembly program? My argument is that, according to Wikipedia, OOP is "based on several techniques, including encapsulation, modularity, polymorphism, and inheritance." The assembly language has no concept of objects, classes, access control, etc.; so how is this possible? And if such a basic foundation (i.e., objects) does not exist, how could one possibly practice polymorphism and inheritance?
titaniumdecoy is offline   Reply With Quote
Old Dec 18th, 2007, 10:54 PM   #2
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: OOP in assembly?

Yes OO is possible in assembly -- Borland had a version of TASM that supported it. I don't know if TASM still does or not.
Ancient Dragon is offline   Reply With Quote
Old Dec 18th, 2007, 10:59 PM   #3
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 15 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
Re: OOP in assembly?

So if I understand correctly, you can only use OOP in assembly if you are using a version that is designed to support it.
titaniumdecoy is offline   Reply With Quote
Old Dec 18th, 2007, 11:23 PM   #4
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: OOP in assembly?

No, I wouldn't say that either. TASM just made it easier to do OOP. You could probably do it with any assembler but it would just be more difficult. All C++ code is compiled down to assembly code, so there is no reason why someone with enough patients can't write it in assembly to begin with. Myself, I wouldn't think of doing such a strange thing
Ancient Dragon is offline   Reply With Quote
Old Dec 18th, 2007, 11:40 PM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 887
Rep Power: 12 Jimbo is on a distinguished road
Re: OOP in assembly?

Doing OOP in assembly is certainly possible (how else would OOP languages get compiled to machine code?) but doing so by hand would likely require an abnormally masochistic personality.
__________________
<insert disclaimer here>
Jimbo is offline   Reply With Quote
Old Dec 18th, 2007, 11:49 PM   #6
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 15 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
Re: OOP in assembly?

Why couldn't OO code can be translated into non-OO assembly code?

Think about the machine code generated from an OOP language by the compiler. How could you possibly call that OO, especially when you consider that for all you know, a similar program written in a functional language could produce the very same machine code?
titaniumdecoy is offline   Reply With Quote
Old Dec 19th, 2007, 12:54 AM   #7
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 887
Rep Power: 12 Jimbo is on a distinguished road
Re: OOP in assembly?

note: vastly oversimplified examples ahead. The assembly examples are pseudo-code as I don't think getting it perfectly right is necessary. It may not reflect how things actually work either, so if anyone feels a dire need to correct me, feel free. Oh yeah, I'm doing this by hand too, just to remind myself how much I hate it...

More notes:
- I forgot x86 calling conventions and BSed them
- I'm using Intel sytnax: instr dest, src
- stack moves down (subtract) to allocate space
- pointer to object points to the end, e.g. struct { int a; int b; }foo; => foo will containt the value of the byte after b. That is, (char*)&foo + 4 is foo.b, (char*)&foo + 8 is foo.a.
- semicolon marks an asm comment
- memory access is done with parens, e.g. (%eax) is the value in memory at %eax

think of it this way: say you have the following C code
typedef struct {
   int a;
   int b;
} foo;
// then in code...
foo f;
f.a = 5;
f.b = 6;
This may end up looking something similar to the following (though the example may be rather inaccurate):
sub %esp, 8 ; allocate space on the stack for the struct.  assuming 4-byte ints
mov %eax, %esp ; load the memory location into eax
mov (%eax + 8), 5 ; save 5 to f.a
mov (%eax + 4), 6 ; save 6 to f.b
This is vastly oversimplified to simply allocating 2 ints on the stack and manipulating them. Let's mix it up a little - a little OOP'ish form in C (another disclaimer: my C is embarassingly rusty, so this may be incorrect as well; consider it pseudo-code ):
typedef struct foobar foo;
struct foobar{
   int a;
   int b;
   int (*init)(foo*, int, int);
};

void noop(foo* f, int a, int b) { }

void initFoo(foo* f, int a, int b) {
   f->a = a;
   f->b = b;
   f->init = noop;
}

// then in code...
foo* f = (foo*)malloc(sizeof(foo));
f->init = initFoo;
f->init(f, 5, 6);
this would yield similar to the following pseudo-asm (for simplicitly, malloc is allocating stack space so I don't have to worry about heap addresses, etc... and again, add a disclaimer for accuracy):
initFoo:
    pop %eax ; get f
    pop %ebx ; get the 5
    pop %ecx ; get the 6
    mov (%eax+12), %ebx ; store the 5 in the first word of f
    mov (%eax+8), %ecx ; store the 6 in the second word of f
    mov (%eax+4), noop ; store &noop in the 3rd word of f
    leave
    ret
noop:
    pop %eax ; remove these from the stack
    pop %ebx ; since they were pushed when
    pop %ecx ; this function was called
    nop ; to have the function live up to it's name
    leave
    ret
; then in code...
    sub %esp, 12 ; allocate room for f
    mov %eax, %esp ; move f to eax
    mov (%eax+4), initFoo ; f->init = initFoo
    push 6 ; load params
    push 5
    push %eax 
    call (%eax+4) ; call f->init(f, 5, 6)
; and at this point, I give up... I think I should have saved f to the stack before calling f->init, since %eax is probably all whacked up
Then for some C++ fun, add vtables, inheritance (think of how this affects memory layout), etc...
__________________
<insert disclaimer here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assembly, Assembler? v0id Assembly 16 Feb 26th, 2009 1:31 PM
quick assembly question jbo423 Assembly 4 Nov 12th, 2007 8:21 AM
Assembly Language DaWei Coder's Corner Lounge 0 Apr 26th, 2007 11:15 PM
GCC Inline Assembly niteice Assembly 0 May 23rd, 2006 5:11 PM
High Level Assembly? Gink Assembly 3 Apr 20th, 2005 12:34 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC