home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 2017 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: easy.in-chemnitz.de!mkmk!floh
  2. From: floh@mkmk.in-chemnitz.de (Andre Weissflog)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: manipulating the stack in C
  5. Message-ID: <Sh++x*aE0@mkmk.in-chemnitz.de>
  6. Date: Fri, 26 Jan 1996 00:42:30 CET
  7. Reply-To: floh@mkmk.in-chemnitz.de
  8. References: <4e8b20$lsq@newshost.lanl.gov>
  9. Distribution: world
  10. Organization: private uucp site
  11. X-Newsreader: Arn V 1.04
  12.  
  13. In article <4e8b20$lsq@newshost.lanl.gov>, Luke Emmert writes:
  14.  
  15. >   I'm currently writing an assembly function that takes a variable
  16. > number of arguments.  The last of the arguments is indicated by forcing
  17. > the stack to point at itself(a low probability occurence so I hope). 
  18. > The function is called from C code and I therefore need to get C to do
  19. > something weird with the stack.  Unfortunately, I need to make
  20. > assumptions about how the compiler will produce binary.
  21. >   The code looks like the following:
  22. > #define LAST_ARG (get_stack())
  23. >   foo( arg1, arg2, arg3, arg4, LAST_ARG );
  24. I think it would be nicer if you give foo() the number of
  25. args first, following the args:
  26.  
  27. void foo(ULONG num_args,...);
  28.  
  29. Since C pushes the last arg first, you will find num_args
  30. always at 4(a7), followed by the "real" arguments starting
  31. at 8(a7):
  32.  
  33.         XDEF foo
  34. foo     lea 4(a7),a0
  35.         movem.l d2-d7,-(a7)
  36.         move.l (a0),d0          ; get <num_args>
  37.         ...
  38.  
  39. > where get_stack() simply returns the current
  40. > stack value:
  41. > _get_stack:
  42. >       move.l   a7,d0
  43. >       rts
  44. Don't forget that the return address is also on the stack, your
  45. _get_stack routine will return the stack pointer 4 bytes too low
  46. (since rts pops 4 bytes).
  47.  
  48. >   The problem arose last night, when I removed a printf() statement
  49. > that I used for debugging.  After disassembling the binary I found that
  50. > the stack was loaded in different ways in each case.  My program
  51. > assumes a particular way of loading the stack and that's why it failed.
  52. >   What I'd like to do is have something in-line that loads the current
  53. > stack value rather like
  54. >   move.l a7,(a7)
  55.  
  56. Hummm? Whazzat?
  57.  
  58. You don't need to adjust the stack pointer yourself, just be sure
  59. to leave the routine with the same stack pointer as it was entered.
  60. The adjustment for the routine's arguments that where pushed onto
  61. the stack will be done on the callers level and is managed
  62. by the compiler.
  63.  
  64. If you have a varargs routine and call it like
  65.  
  66.     foo(1,2,3,4);
  67.  
  68. The compiler will generate something like:
  69.  
  70.     pea 4           ; push values onto stack
  71.     pea 3
  72.     pea 2
  73.     pea 1
  74.     bsr _foo        ; call foo()
  75.     addq.w #16,a7   ; re-adjust stack
  76.  
  77. > without having to use a function call.  I'm currently using SAS C v5.10
  78. > and I'm pretty sure that this cannot be done, but if any one has a
  79. > suggestion I would appreciate it.
  80.  
  81. You could still use getreg(REQ_A7), but I wouldn't necessarly
  82. count on the returned value still beeing valid when you need it.
  83.  
  84. Bye,
  85. -Floh.
  86.  
  87. ====//=== Andre Weissflog <floh@mkmk.in-chemnitz.de> =======
  88. ...// Sep'95: Return Of The Living Death...................
  89. \\// 90% of everything is crap (Sturgeon's Law)...........
  90. =\\===============================================Amiga!=
  91.  
  92.