Or perhaps...

In my example, 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. I assume that the printf routine pops the address of the string off the stack, but leaves the other values on the stack, hence the need to add 2x3=6 to the stack after calling printf in my example.

... just adding 2 for every decrement that was done on sp...

Paul

Paul Riley

Mo: +61 (0)411 781 394



On Mon, 28 Jun 2021 at 12:34, Paul Riley <paul@rileyriot.com> wrote:
Hi,

I want to use printf from an assembly language program, in V6. It seems that the Unix Programmer's Manual doesn't show how to use it from assembly, so I wrote a short C program and captured the assembler output, for some clues. Listings below.

In my example, 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. I assume that the printf routine pops the address of the string off the stack, but leaves the other values on the stack, hence the need to add 2x3=6 to the stack after calling printf in my example.

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. Perhaps I'm not used to PDP-11 stack conventions.

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?

Paul


Paul Riley


int a, b, c;
int main(){
  printf("printf: start\n");
  a = 1;
  b = 2;
  c = 3;
  printf("A = %d, B = %d, C = %d", a, b, c);
  printf("printf: end\n");

}

.comm   _a,2
.comm   _b,2
.comm   _c,2
.globl  _main
.text
_main:
~~main:
jsr     r5,csv
jbr     L1
L2:mov  $L4,(sp)
jsr     pc,*$_printf
mov     $1,_a
mov     $2,_b
mov     $3,_c
mov     _c,(sp)
mov     _b,-(sp)
mov     _a,-(sp)
mov     $L5,-(sp)
jsr     pc,*$_printf
add     $6,sp
mov     $L6,(sp)
jsr     pc,*$_printf
L3:jmp  cret
L1:jbr  L2
.globl
.data
L4:.byte 160,162,151,156,164,146,72,40,163,164,141,162,164,12,0
L5:.byte 101,40,75,40,45,144,54,40,102,40,75,40,45,144,54,40,103,40,75,40,45,144,0
L6:.byte 160,162,151,156,164,146,72,40,145,156,144,12,0
#