Trying to analyse code

Reply

Join Date: Nov 2009
Posts: 1
Reputation: bees1 is an unknown quantity at this point 
Solved Threads: 0
bees1 bees1 is offline Offline
Newbie Poster

Trying to analyse code

 
0
  #1
17 Days Ago
This is an example program that I am studying so that i can make my own mixed mode assembly/C program.

I am having trouble understanding on the assembly code, why I have to push SI twice at different times and only pop it out once. Also why push and pop DI when it's not used. I am confused from when I have push SI the second time.

/* An example to illustrate C function -> assembly -> a C function.
This program calls the assembly language procedure in file MARKS_A.ASM.
The program outputs min, max, and rounded average of two marks. */

  1. #include <stdio.h>
  2. int main(void)
  3. { int mark1,mark2, min, max, av;
  4. int find_avg(int, int);
  5. extern void stats(int, int, int*, int*, int*);
  6. scanf("%d%d",&mark1,&mark2);
  7. stats(mark1,mark2, &min, &max, &av);
  8. printf("Min = %d, Max = %d, Av = %d\n", min, max, av);
  9. return 0; }
  10.  
  11.  
  12. /*********************************************************
  13. * Returns the rounded average required by the assembly
  14. * procedure STATS in file MARKS_A.ASM.*/
  15.  
  16. int find_avg(int total, int number) {
  17. return((int)((double)total/number + 0.5)); }

  1. ;----------- --------- --------- --------- --------- --------- -­
  2. ; Assembly program example to show call to a C function.
  3. ; This procedure receives a marks array and class size
  4. ; and returns minimum, maximum, and rounded average marks.
  5. ;----------- --------- --------- --------- --------- --------- -­
  6. .MODEL SMALL
  7. EXTRN _find_avg:PROC
  8. .CODE
  9. PUBLIC _stats
  10. _stats PROC
  11. push BP
  12. mov BP,SP
  13. push SI
  14. push DI
  15.  
  16.  
  17. mov AX,[BP+4] ; AX := mark1
  18. mov DX,[BP+6] ; DX := mark2
  19. cmp AX,DX ; put min into AX and max into DX
  20. jl next
  21. xchg AX,DX
  22. next:
  23. : mov SI,AX ; SI gets sum
  24. add SI,DX
  25.  
  26. mov BX,[BP+8] ; return minimum
  27. mov [BX],AX
  28. mov BX,[BP+10] ; return maximum
  29. mov [BX],DX
  30.  
  31. ; now call find_avg C function to compute average
  32.  
  33. mov AX,2 ;push number of marks
  34. push AX
  35. push SI ;push sum of marks
  36. call _find_avg ; returns average in AX
  37. add SP,4 ; clear stack
  38. mov BX,[BP+12] ; return average
  39. mov [BX],AX
  40. pop DI
  41. pop SI
  42. pop BP
  43. ret ; NOTE calling program clears stack
  44. _stats ENDP
  45. END
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 33
Reputation: dan63043 is an unknown quantity at this point 
Solved Threads: 4
dan63043 dan63043 is offline Offline
Light Poster
 
0
  #2
17 Days Ago
The C compiler will cause the executable to save and restore certain registers before and after a call, but not others, so the point of this exercise must be to get you thinking along those lines.

Your assembly program calls the external function "find_avg", so you don't know that DI isn't used. The first time you push SI is to preserve it for the return to the C program.

The second time you push SI is to put an argument on the stack for the "find_avg" function. The "add SP,4" effectively removes those arguments from the stack, otherwise the C program would think they were part of the result.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster
 
0
  #3
16 Days Ago
Old time compilers use si and di to store register variables, so they are routinely saved on the stack. It looks to me as if si is being pushed a second time because it is being passed as an argument to the find_avg() function. It, along with ax, is removed with the add sp, 4 instruction. (In 16 and 32 bit C all arguments are passed on the stack).
Last edited by mathematician; 16 Days Ago at 9:00 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC