From mboxrd@z Thu Jan 1 00:00:00 1970 From: erik quanstrom Date: Wed, 12 Feb 2014 11:38:49 -0500 To: 9fans@9fans.net Message-ID: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: Re: [9fans] Strings in acid, what am I missing? Topicbox-Message-UUID: b9208998-ead8-11e9-9d60-3106f5b1d025 > *main:ca\s = *s* > *(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