The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: Paul Riley <paul@rileyriot.com>
To: Noel Chiappa <jnc@mercury.lcs.mit.edu>
Cc: tuhs <tuhs@minnie.tuhs.org>
Subject: Re: [TUHS] Using printf from Assembly Language in V6, and db.
Date: Mon, 28 Jun 2021 21:27:08 +1000	[thread overview]
Message-ID: <CAD05_j3Gz472Oj7tMQUk6nEinVHeb3DvC96kjTupugoa6=RXSw@mail.gmail.com> (raw)
In-Reply-To: <20210628044852.2B53B18C08F@mercury.lcs.mit.edu>

[-- Attachment #1: Type: text/plain, Size: 3451 bytes --]

Noel,

Thanks for that.

As a non-C consumer of printf, should I point R5 at some space for a stack
and call printf in the same manner as the C example I cited? If it's all
too hard I'll stick to writing to stdout with my own routines.

Paul

*Paul Riley*

Email: paul@rileyriot.com




On Mon, 28 Jun 2021 at 14:49, Noel Chiappa <jnc@mercury.lcs.mit.edu> wrote:

>     > 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
>

[-- Attachment #2: Type: text/html, Size: 4808 bytes --]

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

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-28  4:48 Noel Chiappa
2021-06-28 11:27 ` Paul Riley [this message]
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='CAD05_j3Gz472Oj7tMQUk6nEinVHeb3DvC96kjTupugoa6=RXSw@mail.gmail.com' \
    --to=paul@rileyriot.com \
    --cc=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).