tech support cheat sheet
Download
Skip this Video
Download Presentation
Tech Support Cheat Sheet

Loading in 2 Seconds...

play fullscreen
1 / 35

Tech Support Cheat Sheet - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

Tech Support Cheat Sheet. Carnegie Mellon. Carnegie Mellon. Assembly: Arithmetic & Control CSCi 2021: Computer Architecture and Organization. Chap. 3.4 - 3.5. Last Time. Assembly Basics: Registers, operands, move. Carnegie Mellon. Today. Complete addressing mode, address computation

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Tech Support Cheat Sheet' - rayya


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
assembly arithmetic control csci 2021 computer architecture and organization
Carnegie Mellon

Carnegie Mellon

Assembly: Arithmetic & ControlCSCi 2021: Computer Architecture and Organization

Chap. 3.4 - 3.5

last time
Last Time
  • Assembly Basics: Registers, operands, move
today
Carnegie MellonToday
  • Complete addressing mode, address computation
  • Arithmetic operations
  • Control: Condition codes
  • Conditional branches
  • Loops

We will not cover every mode or operation!

Project #2 is out

Exam #1 in one week

Will talk about on Monday/Tuesday

complete memory addressing modes
Carnegie MellonComplete Memory Addressing Modes
  • Most General Form
  • D (Rb,Ri,S) => Mem[Reg[Rb]+S*Reg[Ri]+ D]
    • D: Constant “displacement” 1, 2, or 4 bytes
    • Rb: Base register
    • Ri: Index register
    • S: Scale: 1, 2, 4, or 8
  • Special Cases (no S, S=>1; no D, D=>0)
address computation instruction
Carnegie MellonAddress Computation Instruction
  • lealSrc,Dest
    • Src is address mode expression
    • Set Dest to address denoted by expression
  • Uses
    • Computes addresses with a single instruction => register
      • leal (x, y, k)=> x + k*y
      • k = 1, 2, 4, or 8
  • Example (also used for optimizing arithmetic)

int mul12(int x)

{

return x*12;

}

leal (%eax,%eax,2), %eax ;t <- x+x*2

sall $2, %eax ;return t<<2

today1
Carnegie MellonToday
  • Complete addressing mode, address computation (leal)
  • Arithmetic operations
  • Control: Condition codes
  • Conditional branches
  • Loops
some binary arithmetic operations
Carnegie MellonSome “BINARY” Arithmetic Operations
  • Two Operand Instructions:

FormatComputation

addlSrc,DestDest = Dest + Src

sublSrc,DestDest = DestSrc

imullSrc,DestDest = Dest * Src

sallSrc,DestDest = Dest << SrcAlso called shll

sarlSrc,DestDest = Dest >> SrcArithmetic

shrlSrc,DestDest = Dest >> SrcLogical

xorlSrc,DestDest = Dest ^ Src

andlSrc,DestDest = Dest & Src

orlSrc,DestDest = Dest | Src

  • Watch out for argument order!
  • No distinction between signed and unsigned int (why?)
  • See book for more

why 2?

arithmetic expression example
Carnegie MellonArithmetic Expression Example

arith:

pushl %ebp

movl %esp, %ebp

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

popl %ebp

ret

Set

Up

intarith(int x, int y, int z)

{

int t1 = x+y;

int t2 = z+t1;

int t3 = x+4;

int t4 = y * 48;

int t5 = t3 + t4;

intrval = t2 * t5;

return rval;

}

Body

Finish

understanding arith
Carnegie MellonUnderstanding arith

intarith(int x, int y, int z)

{

int t1 = x+y;

int t2 = z+t1;

int t3 = x+4;

int t4 = y * 48;

int t5 = t3 + t4;

intrval = t2 * t5;

return rval;

}

Offset

%ebp

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

observations about arith
Carnegie MellonObservations about arith

intarith(int x, int y, int z)

{

int t1 = x+y;

int t2 = z+t1;

int t3 = x+4;

int t4 = y * 48;

int t5 = t3 + t4;

intrval = t2 * t5;

return rval;

}

  • Instructions in different order from C code
  • Some expressions require multiple instructions
  • No memory store!

movl 8(%ebp), %ecx # ecx = x

movl 12(%ebp), %edx # edx = y

leal (%edx,%edx,2), %eax # eax = y*3

sall $4, %eax # eax*= 16 (t4)

leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)

addl %ecx, %edx # edx = x+y (t1)

addl 16(%ebp), %edx # edx += z (t2)

imull %edx, %eax # eax = t2 * t5 (rval)

today2
Carnegie MellonToday
  • Complete addressing mode, address computation
  • Arithmetic operations
  • Control: Condition codes
  • Conditional branches
  • Loops
condition codes implicit setting
Carnegie MellonCondition Codes (Implicit Setting)
  • Single bit registers
    • CF Carry Flag – recent op carried (detect unsigned overflow)
    • SF Sign Flag – recent op value was negative
    • ZF Zero Flag – recent op value was 0
    • OF Overflow Flag (detect TC over/under)
  • Implicitly set (think of it as side effect) by arithmetic operations

Example: addlSrc, Dest↔ t = a+b

CF set if carry from most significant bit

ZF set if t == 0

SF set if t < 0 (as signed)

Software can use these flags to conditionally jump, transfer data, set bytes. ALL arith operators set CC (except leal).

condition codes explicit setting compare
Carnegie MellonCondition Codes (Explicit Setting: Compare)
  • Explicit Setting Condition Codes by Compare Instruction
    • cmplSrc2, Src1
    • cmplb,a like computing a-b without setting destination
    • CF set if carry out from most significant bit (used for unsigned comparisons)
    • ZF set if a == b
    • SF set if (a-b) < 0(signed)
reading condition codes
Carnegie MellonReading Condition Codes
  • SetXInstructions
    • setx
    • Set single byte based on combinations of condition codes
reading condition codes1
Carnegie Mellon

Single byte

Reading Condition Codes
  • SetX Instructions:
    • Set single byte based on combination of condition codes
  • One of 8 addressable byte registers
    • Does not alter remaining 3 bytes
    • Typically use movzbl to finish job

intgt (int x, int y)

{

return x > y;

}

Body

movl 12(%ebp),%eax # eax = y

cmpl %eax,8(%ebp) # Compare x : y

setg %al # al = x > y

movzbl %al,%eax # Zero rest of %eax

today3
Carnegie MellonToday
  • Complete addressing mode, address computation (leal)
  • Arithmetic operations
  • Control: Condition codes
  • Conditional branches
  • Loops
jumping
Carnegie MellonJumping
  • jX Instructions
    • Jump to different part of code depending on condition codes

cmp%eax, %ebx

je .L1

addl %ecx, %edx

.L1:

movl %ecx, %edx

in C:

if (a==b)

goto L1;

x += y;

L1:

x = y;

conditional branch
Conditional branch
  • If (test-expr)

then-stmt

else

else-stmt

t = test-expr;

if (!t) gotoFalse;

then-stmt;

goto Done;

False:

else-stmt;

Done:

conditional branch example
CarneeMellonConditional Branch Example

intabsdiff(int x, int y)

{

int result;

if (x > y) {

result = x-y;

} else {

result = y-x;

}

return result;

}

absdiff:

pushl %ebp

movl %esp, %ebp

movl 8(%ebp), %edx

movl 12(%ebp), %eax

cmpl %eax, %edx

jle .L6

subl %eax, %edx

movl %edx, %eax

jmp .L7

.L6:

subl %edx, %eax

.L7:

popl %ebp

ret

Setup

Body1

Body2a

Body2b

%eax holds return result

Finish

today4
Carnegie MellonToday
  • Complete addressing mode, address computation (leal)
  • Arithmetic operations
  • Control: Condition codes
  • Conditional branches
  • Loops
loops
Loops

do-while:

do

body-stmt

while (test-expr);

Loop:

body-stmt;

t = test-expr;

if (t) goto Loop;

while:

while (test-expr)

body-stmt;

t=test-expr;

If (!t) gotoDone;

Loop:

body-stmt;

t = test-expr;

if (t) gotoLoop;

Done:

do while loop example
Carnegie Mellon“Do-While” Loop Example

C Code

intpcount_do(unsigned x) {

int result = 0;

do {

result += x & 0x1;

x >>= 1;

} while (x);

return result;

}

write goto version

exercise
Exercise
  • “goto” versions

intpcount_do(unsigned x)

{

int result = 0;

loop:

result += x & 0x1;

x >>= 1;

if (x)

goto loop;

return result;

}

do while loop compilation
Carnegie Mellon“Do-While” Loop Compilation

Goto Version

  • Registers:

%edxx

%ecxresult

intpcount_do(unsigned x){

int result = 0;

loop:

result += x & 0x1;

x >>= 1;

if (x)

goto loop;

return result;

}

What does this do?

movl $0, %ecx # result = 0

.L2: # loop:

movl %edx, %eax

andl $1, %eax # t = x & 1

addl %eax, %ecx # result += t

shrl %edx # x >>= 1

jne .L2 # If !0, goto loop

while loop example
Carnegie Mellon“While” Loop Example

C Code

GotoVersion

intpcount_while(unsigned x) {

int result = 0;

while (x) {

result += x & 0x1;

x >>= 1;

}

return result;

}

intpcount_do(unsigned x){

int result = 0;

if (!x) goto done;

loop:

result += x & 0x1;

x >>= 1;

if (x)

goto loop;

done:

return result;

}

for loop
For loop

for (init-expr; test-expr; update-expr)

body-stmt;

init-expr;

if (!test-expr)

gotodone;

do {

body-stmt;

update-expr;

} while (test-expr);

done:

for loop example
Carnegie Mellon“For” Loop Example

C Code

intpcount_for(unsigned x) {

inti;

int result = 0;

for (i = 0; i < WSIZE; i++) {

result += 4;

}

return result;

}

summary
Carnegie MellonSummary
  • Today
    • Complete addressing mode, address computation (leal)
    • Arithmetic operations
    • Control: Condition codes
    • Conditional branches
    • Loops
next time
Next Time
  • Next Time
    • Procedures
    • Stack
    • Call / return
    • Chap. 3.7
  • Have a great weekend!
ad