TIP: Click on subject to list as thread! ANSI
echo: cis.os9.68000.osk
to: LARRY OLSON 72227,3467 (X)
from: Kevin Darling 76703,4227
date: 1992-07-02 23:57:55
subject: #15741-#C_Question

 #: 15752 S12/OS9/68000 (OSK)
    02-Jul-92  23:57:55
Sb: #15741-#C_Question
Fm: Kevin Darling 76703,4227
To: LARRY OLSON 72227,3467 (X)

Larry - I'd have never come up with as elegant and simple a solution as Pete,
but I thought I'd add some thoughts.  I know that you are also a 6809 rma
programmer, so another way to help learn C is to look at the compiler output
(eg: "cc test.c -ca" which will stop with a "test.a" source listing).

For example, the goofed up source they gave (shortened up here) was:

main()
{    int x;
     char *a[5];
     for (x = 0; x <= 5; ++x)
          gets(a[x]);
}

Compiled with the -ca option, you get this (#comments are mine):

 psect test_c,0,0,0,0,0
* main()
* {
 ttl main
main:
 pshs u
 ldd #_1
 lbsr _stkcheck

* int x;
* char *a[5];
* for (x = 0; x <= 5; ++x)
 leas -12,s  # make room on stack for int_x and 5 pointers (2+5*2)
 clra
 clrb
 std 10,s    # x=0
 lbra _4     # enter "for" loop

* gets(a[x]);
_2
 ldd 10,s    # x
 aslb        # multiply * 2 as index into array ([x])
 rola        
 leax 0,s    # point to beginning of array
 leax d,x    # now point to a[x]
 ldd 0,x     # pick up a[x]    <<< contains pointer to ?? nothing!
 pshs d      # push on stack as parameter for gets
 lbsr gets   # call gets function
 leas 2,s    # drop parameter
* }
_5
 ldd 10,s    # x = x+1
 addd #1
 std 10,s
_4
 ldd 10,s    # x
 cmpd #5     # <= 5 ?
 lble _2     # yes, loop
_3
 leas 12,s   # else exit
 puls u,pc

_1 equ -82
 endsect

So it was pretty easy to see that the parameter picked up and passed to
the gets function pointed to who-knows-what, yah?   (cont'd in reply)

There are 3 Replies.

SOURCE: compuserve via textfiles.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.