Page 1Alpha Programming CS 740Sept. 15, 2000Topics• Basics• Control Flow• Procedures• Instruction Formats• Flavors of integers• Floating point• Data structures• Byte orderingCS 740 F’00– 2 –Alpha ProcessorsReduced Instruction Set Computer (RISC)• Simple instructions with regular formats• Key Idea: make the common case fast!– infrequent operations can be synthesized using multiple instructionsAssumes compiler will do optimizations• e.g., scalar optimization, register allocation, scheduling, etc.• ISA designed for compilers, not assembly language programmersA 2nd Generation RISC Instruction Set Architecture• Designed for superscalar processors (i.e. >1 inst per cycle)– avoids some of the pitfalls of earlier RISC ISAs (e.g., delay slots)• Designed as a 64-bit ISA from the startVery High Performance Machines• Alpha has been the clear performance leader for many years nowCS 740 F’00– 3 –Translation ProcesstexttextbinarybinarytextCompiler (gcc -S)Assembler (gcc or as)Linker (gcc or ld)Debugger (gdb)disassembled programC program (p1.c p2.c)Asm program (p1.s p2.s)Object program (p1.o p2.o)Executable program (p) libraries (.a)Disassembler (dis -h)CS 740 F’00– 4 –Abstract Machines1) loops2) conditionals3) goto4) Proc. call5) Proc. returnMachine ModelData Control 1) char2) int, float3) double4) struct, array5) pointermem procCmem regs aluprocessorASM1) byte2) word3) doubleword4) contiguous word allocation5) address of initial byte3) branch/jump4) jump & linkPage 2CS 740 F’00– 5 –Alpha Register ConventionGeneral Purpose Registers• 32 total• Store integers and pointers• Fast access: 2 reads, 1 write in single cycleUsage Conventions• Established as part of architecture• Used by all compilers, programs, and libraries• Assures object code compatibility– e.g., can mix Fortran and C$0$1$2$3$4$5$6$7$8$9$10$11$12$13$14$15v0t0t1t2t3t4t5t6t7s0s1s2s3s4s5s6,fpReturn value frominteger functionsTemporaries(not preservedacross procedurecalls)Callee savedFrame pointer, orcallee savedCS 740 F’00– 6 –Registers (cont.)Important Ones for Now$0 Return Value$1..$8 Temporaries$16 First argument$17 Second argument$26 Return address$31 Constant 0$16$17$18$19$20$21$22$23$24$25$26$27$28$29$30$31a0a1a2a3a4a5t8t9t10t11rapv,t12ATgpspzeroInteger argumentsTemporariesCurrent proc addr or TempAlways zeroGlobal pointerStack pointerReserved for assemblerReturn addressCS 740 F’00– 7 –Program RepresentationsC Codelong int gval;void test1(long int x, long int y) {gval = (x+x+x) - (y+y+y);}Compiled to Assembly.align 3.globl test1.ent test1test1:ldgp $29,0($27).frame $30,0,$26,0.prologue 1lda $3,gvaladdq $16,$16,$2addq $2,$16,$2addq $17,$17,$1addq $1,$17,$1subq $2,$1,$2stq $2,0($3)ret $31,($26),1.end test1Obtain with commandgcc -O -S code.cProduces file code.sCS 740 F’00– 8 –Prog. Representation (Cont.)Object0x120001130 <test1>:0x27bb20000x23bd6f300xa47d80980x421004020x405004020x423104010x403104010x404105220xb44300000x6bfa8001Disassembled0x120001130 <test1>: ldah gp,536870912(t12)0x120001134 <test1+4>: lda gp, 28464(gp)0x120001138 <test1+8>: ldq t2, -32616(gp)0x12000113c <test1+12>: addq a0, a0, t10x120001140 <test1+16>: addq t1, a0, t10x120001144 <test1+20>: addq a1, a1, t00x120001148 <test1+24>: addq t0, a1, t00x12000114c <test1+28>: subq t1, t0, t10x120001150 <test1+32>: stq t1, 0(t2)0x120001154 <test1+36>: ret zero, (ra), 1Run gdb on object codex/10 0x120001130– Print 10 words in hexadecimal starting at address 0x120001130dissassemble test1– Print disassembled version of procedurePage 3CS 740 F’00– 9 –Alternate DisassemblyAlpha program “dis”dis file.o• Prints disassembled version of object code file• The “-h” option prints hardware register names (r0–r31)• Code not yet linked– Addresses of procedures and global data not yet resolvedtest1:0x0: 27bb0001 ldah gp, 1(t12)0x4: 23bd8760 lda gp, -30880(gp)0x8: a47d8010 ldq t2, -32752(gp)0xc: 42100402 addq a0, a0, t10x10: 40500402 addq t1, a0, t10x14: 42310401 addq a1, a1, t00x18: 40310401 addq t0, a1, t00x1c: 40410522 subq t1, t0, t10x20: b4430000 stq t1, 0(t2)0x24: 6bfa8001 ret zero, (ra), 1CS 740 F’00– 10 –Returning a Value from a ProcedurePlace result in $0long int test2(long int x, long int y){return (x+x+x) - (y+y+y);}Compiled to Assembly.align 3.globl test2.ent test2test2:.frame $30,0,$26,0.prologue 0addq $16,$16,$1addq $1,$16,$1addq $17,$17,$0addq $0,$17,$0subq $1,$0,$0ret $31,($26),1.end test2C CodeCS 740 F’00– 11 –Pointer ExamplesC Codelong int iaddp(long int *xp,long int *yp){int x = *xp;int y = *yp;return x + y; }Annotated Assemblyiaddp:ldq $1,0($16) # $1 = *xpldq $0,0($17) # $0 = *ypaddq $1,$0,$0 # return with a ret $31,($26),1 # value x + yvoidincr(long int *sum, long int v){long int old = *sum;long int new = old+val;*sum = new;}incr:ldq $1,0($16) # $1 = *sumaddq $1,$17,$1 # $1 += vstq $1,0($16) # *sum = $1ret $31,($26),1 # returnCS 740 F’00– 12 –Array IndexingC Codelong int arefl(long int a[], long int i){return a[i];}Annotated Assemblyarefl:s8addq $17,$16,$17 # $17 = 8*i + &a[0]ldq $0,0($17) # return val = a[i]ret $31,($26),1 # returnintarefi(int a[], long int i){return a[i];}arefi:s4addq $17,$16,$17 # $17 = 4*i + &a[0]ldl $0,0($17) # return val = a[i]ret $31,($26),1 # returnPage 4CS 740 F’00– 13 –Array Indexing (Cont.)C Code Annotated Assemblylong int garray[10];long int gref(long int i){return garray[i];}.comm garray,80gref:ldgp $29,0($27) # setup the gplda $1,garray # $1 = &garray[0]s8addq $16,$1,$16 # $16 = 8*i + $1ldq $0,0($16) # ret val = garray[i]ret $31,($26),1 # return0x80 <gref>: 27bb0001 ldah gp, 65536(t12)0x84 <gref+4>: 23bd86e0 lda gp, -31008(gp)0x88 <gref+8>: a43d8018 ldq t0, -32744(gp)0x8c <gref+12>: 42010650 s8addq a0, t0, a00x90 <gref+16>: a4100000 ldq v0, 0(a0)0x94 <gref+20>: 6bfa8001 ret zero, (ra), 1Disassembled:CS 740 F’00– 14 –Structures & PointersC Codestruct rec {long int i;long int a[3];long int *p;};Annotated Assemblyset_i:stq $17,0($16) # r->i = valret $31,($26),1void set_i(struct rec *r,long int val){r->i = val;}i a p0 8 32CS 740 F’00– 15 –Structures & Pointers (Cont.)C Codestruct rec {long int i;long int a[3];long int *p;};Annotated Assemblyfind_a:s8addq $17,8,$0 # $0 = 8*idx + 8addq $16,$0,$0 # $0 += rret $31,($26),1long int *find_a(struct rec *r, long int idx){return &r->a[idx];}i a p0 8 32CS 740 F’00– 16 –Structures & Pointers (Cont.)C Codestruct rec {long int i;long
View Full Document