Docsity

CompE271 - study notes asm x86, Study notes of Computer Science

Toggle sidebar
bg1
Machine-Level Programming II: Arithmetic & Control - Study Notes
Lecture Topics:
- Complete Addressing Mode, leal Instruction
- Arithmetic Operations (2-operand, 1-operand)
- Control: Condition Codes
- Conditional Branches
- Loop Constructs
Address Computation:
- leal Src, Dest: Computes address without memory reference
- Useful for:
* p = &x[i]
* Arithmetic expressions like x + k*y (k=1,2,4,8)
Example:
int mul12(int x) { return x*12; }
leal (%eax,%eax,2), %eax // x*3
sall $2, %eax // x*12
Arithmetic Instructions:
- 2-Operand: addl, subl, imull, sall, sarl, shrl, xorl, andl, orl
- 1-Operand: incl, decl, negl, notl
- XOR is commonly used to zero a register: xorl %eax, %eax
Example: Complex Expression
int arith(int x, int y, int z)
{
int rval = (x+y+z) * (x+4+48*y);
return rval;
}
Assembly:
movl 8(%ebp), %ecx
movl 12(%ebp), %edx
leal (%edx,%edx,2), %eax
sall $4, %eax
leal 4(%ecx,%eax), %eax
addl %ecx, %edx
addl 16(%ebp), %edx
imull %edx, %eax
Condition Codes:
- Set implicitly by arithmetic: CF, ZF, SF, OF
- Set explicitly via:
* cmpl Src2, Src1 (a - b)
* testl Src2, Src1 (a & b)
Read via:
- setX (sete, setne, sets, setns, setg, setge, setl, setle, seta, setb)
- jX (jmp, je, jne, js, jns, jg, jge, jl, jle, ja, jb)
pf2

Partial preview of the text

Download CompE271 - study notes asm x86 and more Study notes Computer Science in PDF only on Docsity!

Machine-Level Programming II: Arithmetic & Control - Study Notes

Lecture Topics:

  • Complete Addressing Mode, leal Instruction
  • Arithmetic Operations (2-operand, 1-operand)
  • Control: Condition Codes
  • Conditional Branches
  • Loop Constructs

Address Computation:

  • leal Src, Dest: Computes address without memory reference
  • Useful for:
    • p = &x[i]
    • Arithmetic expressions like x + ky (k=1,2,4,8) Example: int mul12(int x) { return x12; } leal (%eax,%eax,2), %eax // x* sall $2, %eax // x*

Arithmetic Instructions:

  • 2-Operand: addl, subl, imull, sall, sarl, shrl, xorl, andl, orl
  • 1-Operand: incl, decl, negl, notl
  • XOR is commonly used to zero a register: xorl %eax, %eax

Example: Complex Expression int arith(int x, int y, int z) { int rval = (x+y+z) * (x+4+48*y); return rval; }

Assembly: movl 8(%ebp), %ecx movl 12(%ebp), %edx leal (%edx,%edx,2), %eax sall $4, %eax leal 4(%ecx,%eax), %eax addl %ecx, %edx addl 16(%ebp), %edx imull %edx, %eax

Condition Codes:

  • Set implicitly by arithmetic: CF, ZF, SF, OF
  • Set explicitly via:
    • cmpl Src2, Src1 (a - b)
    • testl Src2, Src1 (a & b)

Read via:

  • setX (sete, setne, sets, setns, setg, setge, setl, setle, seta, setb)
  • jX (jmp, je, jne, js, jns, jg, jge, jl, jle, ja, jb)

Conditional Branch Example: int absdiff(int x, int y) { if (x > y) return x - y; else return y - x; }

Loops:

  • do-while: do { Body } while(Test);
  • while: while(Test) { Body }
  • for loop: for (Init; Test; Update) { Body } Translated to: Init; while(Test) { Body; Update; }
  • goto form for all loops also shown

Examples:

  • pcount_do, pcount_while, pcount_for Count number of 1s in unsigned int

Summary:

  • Arithmetic ops
  • Control: condition codes
  • Conditional branches
  • Loops (do-while, while, for)