--*- coding: utf-8; -*-- L?sningsforslag til ukeoppgaver 27. oktober og 1. november 2011 Oppgave 1 --------- 1a v1 = -17; Siste movl: parametrene har byttet plass. Korrekt skal v?re: movl $-17,%eax # -17 movl %eax,v1 # v1 = 1b v2 = v1 - 1; Glemt pushl og popl rundt beregningen av andre operand (1); korrekt skal v?re: movl v1,%eax # v1 pushl %eax movl $1,%eax # 1 movl %eax,%ecx popl %eax subl %ecx,%eax # Compute - movl %eax,v2 # v2 = 1c v3 = v1*-2; Instruksjonen for multiplikasjon heter imull (ikke mull); korrekt er: movl v1,%eax # v1 pushl %eax movl $-2,%eax # -2 movl %eax,%ecx popl %eax imull %ecx,%eax # Compute * movl %eax,v3 # v3 = 1d v4 = 2*(v2 + 2); Glemt en ?movl %eax,%ecx?; korrekt skal v?re: movl $2,%eax # 2 pushl %eax movl v2,%eax # v2 pushl %eax movl $2,%eax # 2 movl %eax,%ecx popl %eax addl %ecx,%eax # Compute + movl %eax,%ecx popl %eax imull %ecx,%eax # Compute * movl %eax,v4 # v4 = 1e v5 = (v1*3) / (v2*2); Glemt cdq-instruksjonen; korrekt er: movl v1,%eax # v1 pushl %eax movl $3,%eax # 3 movl %eax,%ecx popl %eax imull %ecx,%eax # Compute * pushl %eax movl v2,%eax # v2 pushl %eax movl $2,%eax # 2 movl %eax,%ecx popl %eax imull %ecx,%eax # Compute * movl %eax,%ecx popl %eax cdq idivl %ecx # Compute / movl %eax,v5 # v5 = Oppgave 2 --------- class EmptyStatm extends Statement { @Override void genCode(FuncDecl curFunc) { // No code necessary } : } class ArithOperator extends Operator { @Override void genCode(FuncDecl curFunc) { Code.genInstr("", "movl", "%eax,%ecx", ""); Code.genInstr("", "popl", "%eax", ""); switch (opToken) { case addToken: Code.genInstr("", "addl", "%ecx,%eax", "Compute +"); break; case subtractToken: Code.genInstr("", "subl", "%ecx,%eax", "Compute -"); break; case multiplyToken: Code.genInstr("", "imull", "%ecx,%eax", "Compute *"); break; case divideToken: Code.genInstr("", "cdq", "", ""); Code.genInstr("", "idivl", "%ecx", "Compute /"); break; } } : } Oppgave 3 --------- Fordi en operand kan v?re et uttrykk i parenteser, som i 2 * (b+1)