9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: Re: [9fans] Strings in acid, what am I missing?
Date: Wed, 12 Feb 2014 11:38:49 -0500	[thread overview]
Message-ID: <edf1c59a4c2b0756af6b6aaacf733b15@brasstown.quanstro.net> (raw)
In-Reply-To: <c11424dd8047058471818b89494b8793@yourdomain.dom>

> *main:ca\s = *s<integer>*
> *(main:ca\s) = Hello
> *(main:cp\s) = $
>
> The first three values are expected, but when it gets to strings, I
> fail to understand what's happening.  What is the purpose of the
> parenthenses?  Why does 'cp' not give me the correct value and just
> gives me garbage instead?

part of the issue is, c does not have strings.  the other part is that
acid doesn't really understand c.  the reason that the () are required
is that * binds tighter than \s in acid.  one thing that's confusing about
acid is main:ca is an *address* of main:ca, not its value.  so *main:ca
is the pointer into the bss, and main:ca is the address of that pointer.

the reason cp gives you garbage is it's not initialized.  in fact when
i compile this for am64, you can see everthing quite clearly.  it is
optimized away!  (it's a good question why a char* is treated differently
than a char[].  perhaps there is some dark corner of the standard that
implies things.)

acid; asm(main)
main 0x00200028	SUBQ	$0x38,SP
main+0x4 0x0020002c	MOVL	$0xa,x+0x34(SP)		// x = 10
main+0xc 0x00200034	LEAQ	x+0x34(SP),AX		// ip = &x
main+0x11 0x00200039	MOVL	$0x1,ia+0x1c(SP)		// ia[0] = 1
main+0x19 0x00200041	MOVL	$0x2,0x20(SP)		// ia[1] = 2
main+0x21 0x00200049	MOVL	$0x3,0x24(SP)		// ia[2] = 3
main+0x29 0x00200051	MOVB	$0x48,ca+0x16(SP)	// ca[0] = 'H'
main+0x2e 0x00200056	MOVB	$0x65,0x17(SP)		// ca[1] = 'e'
main+0x33 0x0020005b	MOVB	$0x6c,0x18(SP)		// ca[1] = 'l'
main+0x38 0x00200060	MOVB	$0x6c,0x19(SP)		// ca[1] = 'l'
main+0x3d 0x00200065	MOVB	$0x6f,0x1a(SP)		// ca[1] = 'o'
main+0x42 0x0020006a	MOVB	$0x0,0x1b(SP)		// ca[1] = '\0'
main+0x47 0x0020006f	MOVL	$.string(SB),AX
main+0x4c 0x00200074	XORQ	BP,BP			// 1st argument to exits -> 0
main+0x4f 0x00200077	CALL	exits(SB)
main+0x54 0x0020007c	ADDQ	$0x38,SP
main+0x58 0x00200080	RET
_main 0x00200081	SUBQ	$0x90,SP

you can also see this in the compile output:

; tmk q.c
6c -FVTw q.c
warning: q.c:11 auto declared and not used: cp
warning: q.c:10 auto declared and not used: ca
warning: q.c:9 auto declared and not used: ia
warning: q.c:8 auto declared and not used: ip
warning: q.c:8 set and not used: ip
warning: q.c:11 set and not used: cp
6l -o 6.q q.6

- erik



  reply	other threads:[~2014-02-12 16:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-12 16:21 Grant R. Mather
2014-02-12 16:38 ` erik quanstrom [this message]
2014-02-14  1:38   ` Grant R. Mather
2014-02-14  1:44     ` erik quanstrom

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=edf1c59a4c2b0756af6b6aaacf733b15@brasstown.quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /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).