home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / mac / programm / 19749 < prev    next >
Encoding:
Internet Message Format  |  1992-12-13  |  3.2 KB

  1. Path: sparky!uunet!europa.asd.contel.com!howland.reston.ans.net!usc!wupost!spool.mu.edu!agate!stanford.edu!apple!goofy!mumbo.apple.com!gallant.apple.com!seuss.apple.com!user
  2. From: absurd@apple.apple.com (Tim Dierks, software saboteur)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Assembly with C question...
  5. Message-ID: <absurd-131292134029@seuss.apple.com>
  6. Date: 13 Dec 92 22:07:23 GMT
  7. References: <1992Dec12.232559.265@physc1.byu.edu>
  8. Sender: news@gallant.apple.com
  9. Followup-To: comp.sys.mac.programmer
  10. Organization: MacDTS Marauders
  11. Lines: 71
  12.  
  13. In article <1992Dec12.232559.265@physc1.byu.edu>, seth@physc1.byu.edu
  14. wrote:
  15. > Hi. I have a question about assembly language and C. Here is a snippet of
  16. > C code, followed by the same thing disassembled.
  17. >  
  18. >  
  19. > main()
  20. > {
  21. >     Init();
  22. >     MakeMenus();
  23. >     MakeWindow();
  24. >     AddrsSet();
  25. >     MakeGrid();
  26. >  
  27. >  
  28. > main:
  29. > 00000000        JSR       $0000(A5)
  30. > 00000004        JSR       $0000(A5)
  31. > 00000008        JSR       $0000(A5)
  32. > 0000000C        JSR       $0000(A5)
  33. > 00000010        JSR       $0000(A5)
  34. >  
  35. >  
  36. > My question is, why are all the function calls done in this way? How can I 
  37. > tell what the offset relative to a5 is? I am confused. I have till now only
  38. > done assembly in the MDS (sad, sad) assembler. (yes, I was the one whining
  39. > about it a few weeks ago. The class is over, and now I can do assembly with 
  40. > the inline assembler of THINK C like so many of you suggested...)
  41. > In the MDS Assembler we always could tell just what our offsets were, 
  42. > becaused we used labels. I don't understand how all these function calls can
  43. > be made with an offset of $0000(a5). Isn't a5 the global area? Why are the 
  44. > function calls going there anyways? Shouldn't they be going to the code area,
  45. > with offsets relative to the PC? I obviously don't see what is supposed to be
  46. > going on here, and I would appreciate it if one of you assembly gurus would 
  47. > enlighten me...
  48. >  
  49. > Thanks,
  50. > Seth Leigh
  51. > BYU Physics Dept.
  52.  
  53. Here's the poop:
  54. (I don't know why that's there, I guess I just wanted to say "here's the
  55. poop", just once in my life.  Forgive me.)
  56.  
  57. Q: Why are all the offsets zero?
  58. A: You're looking at the assembly generated after compiling, not after
  59. linking.
  60. Because function offsets don't get generated until the program is linked,
  61. all
  62. the offsets are still 0; the zeros ar just placeholders for values which
  63. will
  64. get filled in by the linker later.  If you want to see fully assembled &
  65. linked
  66. code, build your application and then look at the code with the ResEdit
  67. CODE
  68. Viewer (available at better ftp sites) or with MacsBug; you'll see the
  69. references in all their gory detail.
  70.  
  71. Q: Why are they A5-relative addresses?
  72. A: There are two things (at least) stored in the A5 storage area: globals,
  73. as you are aware, and the jump table, which allows functions in one segment
  74. to call functions in another segment.  The jump table contains code which
  75. links the segments together, loading them as necessary.  Thus, to call a
  76. function in another segment, you jump to its jump table entry, which is
  77. generally at a positive offset from A5.  If the function is in the same
  78. segment (which isn't determined until link time), then the call might
  79. get converted to PC-relative (this depends on a number of factors).
  80.  
  81. Tim Dierks
  82. MacDTS, but today my shoes match!
  83.