Tech support cheat sheet
This presentation is the property of its rightful owner.
Sponsored Links
1 / 35

Tech Support Cheat Sheet PowerPoint PPT Presentation


  • 69 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Tech Support Cheat Sheet

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


Tech support cheat sheet

Tech Support Cheat Sheet


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 Mellon

Today

  • 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 Mellon

Complete 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 examples

Carnegie Mellon

Address Computation Examples


Address computation instruction

Carnegie Mellon

Address 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 Mellon

Today

  • Complete addressing mode, address computation (leal)

  • Arithmetic operations

  • Control: Condition codes

  • Conditional branches

  • Loops


Some binary arithmetic operations

Carnegie Mellon

Some “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 Mellon

Arithmetic Expression Example

arith:

pushl%ebp

movl%esp, %ebp

movl8(%ebp), %ecx

movl12(%ebp), %edx

leal(%edx,%edx,2), %eax

sall$4, %eax

leal4(%ecx,%eax), %eax

addl%ecx, %edx

addl16(%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 Mellon

Understanding 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

movl8(%ebp), %ecx

movl12(%ebp), %edx

leal(%edx,%edx,2), %eax

sall$4, %eax

leal4(%ecx,%eax), %eax

addl%ecx, %edx

addl16(%ebp), %edx

imull%edx, %eax


Observations about arith

Carnegie Mellon

Observations 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!

movl8(%ebp), %ecx# ecx = x

movl12(%ebp), %edx# edx = y

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

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

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

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

addl16(%ebp), %edx# edx += z (t2)

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


Today2

Carnegie Mellon

Today

  • Complete addressing mode, address computation

  • Arithmetic operations

  • Control: Condition codes

  • Conditional branches

  • Loops


Condition codes implicit setting

Carnegie Mellon

Condition 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 Mellon

Condition 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 Mellon

Reading Condition Codes

  • SetXInstructions

    • setx<single_byte_register>

    • 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


On exams

On Exams …


Today3

Carnegie Mellon

Today

  • Complete addressing mode, address computation (leal)

  • Arithmetic operations

  • Control: Condition codes

  • Conditional branches

  • Loops


Jumping

Carnegie Mellon

Jumping

  • 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

CarneeMellon

Conditional 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 Mellon

Today

  • 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;

}


Exercise transformed c code

Exercise: transformed C code

  • On your own …


Summary

Carnegie Mellon

Summary

  • 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!


  • Login