The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: jnc@mercury.lcs.mit.edu (Noel Chiappa)
To: tuhs@minnie.tuhs.org
Cc: jnc@mercury.lcs.mit.edu
Subject: Re: [TUHS] Using printf from Assembly Language in V6, and db.
Date: Mon, 28 Jun 2021 00:48:52 -0400 (EDT)	[thread overview]
Message-ID: <20210628044852.2B53B18C08F@mercury.lcs.mit.edu> (raw)

    > From: Paul Riley

    > I want to use printf from an assembly language program, in V6. ...  the
    > substitutional arguments for printf are pushed onto the stack in reverse
    > order, then the address of the string, and then printf is called. After
    > this, 6 is added to the stack pointer.

This is all down to the standard C environment / calling sequence on the
PDP-11 (at least, in V6 C; other compilers may do it differently). Calls to
printf() are in no way special.

Very briefly, there's a 'frame pointer' (R5) which points to the current stack
frame; all arguments and automatics are relative to that. A pair of special
routines, csv and cret (I couldn't find the source on TUHS, but it happens to
be here:

  http://ana-3.lcs.mit.edu/~jnc/tech/unix/lib/csv.s

if you want to see it), set up and tear down the frame on entry/exit to that
routine. The SP (R6) points to a blank location on the top (i.e. lower address;
PDP-11 stacks grow down) of the stack.

To call a subroutine, the arguments are pushed, the routine is called (which
pushes the return PC), and on return (which pops the return PC), the arguments
are discarded by the caller.

(I wrote a note, BITD, explaining how all this worked; I'll upload it to the
CHWiki when I get a chance.)


    > I assume that the printf routine pops the address of the string off the
    > stack, but leaves the other values on the stack

No, all C routines (including printf()) leave the stack more or less alone,
except for CSV/CRET hackery, allocating space for automatic variables on
routine entry (that would be at L1; try looking at the .s for a routine with
automatic variables), and popping the return PC on exit. The exception to this
is the stuff around calling _enother_ routine (sketched above).

Another exception is alloca() (source here:

  http://ana-3.lcs.mit.edu/~jnc/tech/unix/lib/alloca.s

again, couldn't find it in TUHS), which allocated a block of memory on
the stack (automatically discarded when the routine which called alloca()
returns). Note that since all automatic variables and incoming arguments
are relative to the FP, alloca is _really_ simple; justs adjusts the
SP, and it's done.

    > What troubles me is that the stack pointer is not decremented before the
    > first mov, in the example below. Is this some C convention? I would
    > assume that the first push in my example would overwrite the top of the
    > stack.

That's right; that's because in the C runtime environment, the top location
on the stack is a trash word (set up by csv).

    > I understand db only works on files like a.out or core dumps. If I
    > wanted to break the assembly language program to examine values, how can
    > I force a termination and core dump elegantly, so I can examine some
    > register values?

Use 'cdb':

  https://minnie.tuhs.org//cgi-bin/utree.pl?file=V6/usr/man/man1/cdb.1

which can do interactive debugging.

      Noel

             reply	other threads:[~2021-06-28  5:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-28  4:48 Noel Chiappa [this message]
2021-06-28 11:27 ` Paul Riley
2021-06-28 13:48   ` Clem Cole
  -- strict thread matches above, loose matches on Subject: below --
2021-06-28 20:37 Noel Chiappa
2021-06-29 12:18 ` Paul Riley
2021-06-28  2:34 Paul Riley
2021-06-28  3:27 ` Paul Riley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210628044852.2B53B18C08F@mercury.lcs.mit.edu \
    --to=jnc@mercury.lcs.mit.edu \
    --cc=tuhs@minnie.tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).