Assembly programming

Jun 26, 2008 at 8:47pm
If I have some questions about assembly programming, where do I post?
Jun 26, 2008 at 8:53pm
On an assembly forum :P
Jun 26, 2008 at 9:02pm
Yeeeeeah...heh heh I was kinda hoping...i would get some insight from here :)
Jun 26, 2008 at 9:09pm
You can post specific questions in the beginner forum. I am not too sure what sort of answers you will get. I know a bit of assembly, some other guys will not doubt know a little bit as well.

But wth are you wanting to learn ASM?
Jun 26, 2008 at 11:37pm
Kudos on wanting to learn assembly. It isn't as friendly as a structured, high-level language though.

You might want to check out
http://www.masm32.com/

There are other assemblers also, like gas and nasm, to name two popular ones.

In general, C and C++ compilers are capable of producing assembly output as good as (or better than) what a human can do with an assembler.

And to echo Zaita, what do you want to learn it for?
Jun 27, 2008 at 4:39pm
@Zaita/Duoas,

Hi. Well, you know, I am also interested in the low level workings of our slave (the computer) ;).

Jun 27, 2008 at 5:13pm
Are you interested in understanding how it (the slave;) works at a more fundamental level or creating something specific: device drivers, etc?

:)
Jun 27, 2008 at 11:27pm
@Cplusplussdna,

Interested in the fundamental level. In this context, I have written a small program and have documented it, perhaps somebody (or you) can see if its ok.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//assume callee needs three registers %ebx %edi %esi*/
   int func(int n1, int n2) {
   int temp = n1 * n2;
   return temp - (n1 + n2);
   }
   

   //caller %eax %edx %ecx
   int main()
   {
   int a = 4;
   int b = 8;
   int c = func(a, b);
   return 0;
   }


   /*

// my work here, can someone please help in verifying it?  //Basically, I dont want to overwrite the callee before saving 
// the values

pushl   %ebp                   
movl    %esp, %ebp         
movl    8(%ebp), %ecx      ;; addressing modes -- copy a into ecx 
movl    12(%ebp), %edx	   ;; addressing modes -- copy b into edx	
pushl   %edi						;; need to be pushed onto the stack before altering them
push    %esi						;; need to be pushed onto the stack before altering them
push    %ebx						;; need to be pushed onto the stack before altering them
movl    %edx,%edi				;; copy b on edi
movl		%ecx, %esi			;;	copy a on esi
imull  (%esi,%edi), %ebx  ;; multiply a * b and store value in ebx
subl   %esi, %ebx				;; subtract a from temp
subl   %edi, %ebx				;; subtract b from temp
movl  %ebx, %eax				;; set up return value
popl   %edi						;; return
popl   %esi						;; return
popl   %ebx						;; return
movl    %ebp, %esp         ;; reset the stack pointer
popl    %ebp               ;; reset the frame pointer
ret                        ;; returnThanks for your help
 */

Jun 28, 2008 at 2:23am
You need to try assembling it and see what error messages gas will give you.

Line 32 looks suspect. (You made me pull out my manual.) There is no IMUL opcode that takes three r/m32 operands. What x86 instruction reference are you using? (I'll take a look over it to give better answers.)

Lastly, the multiplication opcodes have implicit operand modes which are typically very useful.

One last thing: on the Intel processors each register has specific characteristics that make it particularly useful for certain tasks (or the only one usable for a given task):
EAX -- accumulator, return values, temporaries, etc.
EBX -- base (from which other registers are offset. Often used to index the stack)
ECX -- counter (the only register you can use for loops and string operations)
EDX -- general data
ESI -- source index (paired with EDS for string operations)
EDI -- destination index (paired with EES or EDS for string operations, depending on instruction used)
ESP -- stack pointer (you already know that)
EBP -- base pointer (used for subroutine stack frames)

hmm... what else, segment registers ECS, EDS, EES, EFS, EGS... that's all I can think of off the top of my head right now.

The way I would have translated the routine is (well, I would have made it a little more efficient, but this will do):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.text
.globl func

func:

; // stack frame: you did this right
pushl   %%ebp
movl    %%esp, %ebp

; // eax := temp = n1 * n2

movl    8(%%ebp),  %%eax  ; // temp  = n1 
imull   12(%%ebp), %%eax  ; // temp *= n2 

; // temp -= (n1 + n2)

subl    8(%%ebp),  %%eax  ; // temp -= n1 
subl    12(%%ebp), %%eax  ; // temp -= n2 

; // return temp

popl    %%ebp
ret

This function assumes __cdecl calling convention. So if you assemble it into an object file and link it into your C application, you would use it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

extern __cdecl int func( int n1, int n2 );

int main()
  {
  int a, b;

  printf( "%s", "Please enter two integer numbers> " );
  scanf( "%d %d", &a, &b );
  printf( "%s %d\n", "The result of 'func( a, b )' is", func( a, b ) );

  return 0;
  }

Notice how I kept register usage down, and I used EAX for the things it does best.

I can't stand AT&T syntax. I believe you can get GAS to take Intel syntax if you like.

I have pretty much typed this in off the top of my head. Mistakes may have occurred.

Hope this helps.
Last edited on Jun 28, 2008 at 2:26am
Jun 28, 2008 at 1:31pm
@Duoas,

You are right about imull, and I have rectified that error. Thanks a lot. As for the rest, I could have made use of %eax, %ecx and %edx only, but I made use of the callee save registers for showing the process full circle (for myself, that is) :).

Thanks a lot for your help, so judging by your comment, only line 32 looks suspect to you? or there could be someone else?

Jun 28, 2008 at 2:11pm
Well, I hadn't really traced through it, but another quick glance shows that lines 36..38 are in the wrong order. You must always POP in the opposite order that you PUSH.

Hope this helps.
Jun 28, 2008 at 4:08pm
@Duoas,

Yup, you are right again, I corrected it. Thanks!!!

Jun 29, 2008 at 7:03pm
ladesidude: It's a bit of a read, but get ahold of the book/e-book called "The Art of Assembly". That will give you a VERY VERY good foundation on Assembly and the inner workings of the CPU (how it actually does stuff in binary etc).
Topic archived. No new replies allowed.