1 / 23

Introduction to Assembly Language IA32-III

Introduction to Assembly Language IA32-III. Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University. Macro’s. The commands .macro and . endm allow you to define macros that generate assembly output

Télécharger la présentation

Introduction to Assembly Language IA32-III

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Assembly Language IA32-III Summer2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

  2. Macro’s • The commands .macro and .endm allow you to define macros that generate assembly output • It may allow to pass the parameters too separated by comma or space .macro sum a b movl \a, %eax mov \b, %ebx addl %ebx, %eax .endm Introduction

  3. Sample • The macros may be used as: .macro write strstr_size movl $4, %eax movl $1, %ebx movl \str, %ecx movl \str_size, %edx int $0x80 .endm .macro writenumstr movl $4, %eax movl $1, %ebx movl \str, %ecx movl $1, %edx int $0x80 .endm .macro read num movl $3, %eax movl $0, %ebx movl \num, %ecx movl $2, %edx int $0x80 .endm write $prompt_str1, $STR1_SIZE read $input1 write $prompt_str2, $STR2_SIZE read $input2 movl input1, %eax subl $0x30, %eax mov input2, %ebx subl $0x30, %ebx addl %ebx, %eax addl $0x30, %eax movl %eax, ans write $prompt_str3, $STR3_SIZE writenum$ans Introduction

  4. Carnegie Mellon Condition Codes (Explicit Setting: Compare) • Explicit Setting by Compare Instruction • cmplSrc2, Src1 • cmplb,a like computing a-b without setting destination, i.e., compare a to b • ZF set if (a == b) • SF set if (a - b) < 0 (as signed, i.e., a < b) • Jump instructions use these flags for controlling program execution, i.e., conditional branching. • CF set if carry out from most significant bit (used for unsigned comparisons) • OF set if two’s-complement (signed) overflow(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

  5. Jump commands • Jump with the conditions: • Jmp label Introduction

  6. Carnegie Mellon Condition Codes (Explicit Setting: Test) • Explicit Setting by Testinstruction • testlSrc2, Src1testl b,a like computing a&b without setting destination • Sets condition codes based on value of Src1 & Src2 • Useful to have one of the operands be a mask • ZF set when a&b == 0 • SF set when a&b < 0

  7. Jump commands • Loop commands Introduction

  8. Carnegie Mellon Conditional Branch Example int absdiff(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 x y Body1 Body2a Body2b • Can you rewrite the above code using goto statements? Finish Can you write C code?

  9. Carnegie Mellon intgoto_ad(int x, int y) { int result; if (x <= y) gotoElse; result = x-y; goto Exit; Else: result = y-x; Exit: return result; } • C allows “goto” as means of transferring control • Closer to machine-level programming style • Generally considered bad coding style in high-level programming. 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 x y Body1 Body2a Body2b Finish

  10. Carnegie Mellon int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; goto Exit; Else: result = y-x; Exit: 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 x y Body1 Body2a Body2b Finish

  11. Carnegie Mellon int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; goto Exit; Else: result = y-x; Exit: 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 x y Body1 Body2a Body2b Finish

  12. Carnegie Mellon int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; goto Exit; Else: result = y-x; Exit: 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 x y Body1 Body2a Body2b Finish

  13. Carnegie Mellon “Do-While” Loop Example • Count number of 1’s in argument x (“popcount”) • Use conditional branch to either continue looping or to exit loop C Code Goto Version (if (…) goto …) int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } int pcount_do(unsigned x) { int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; }

  14. Carnegie Mellon “Do-While” Loop Compilation Goto Version (if (…) goto …) Non-goto Version (if (…) goto …) • Registers: %edxx %ecxresult int pcount_do(unsigned x){ int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; } int pcount_do(unsigned x){ int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } 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

  15. Carnegie Mellon General “Do-While” Translation Goto Version (if (…) goto …) C Code • Body: • Test returns integer • = 0 interpreted as false • ≠ 0 interpreted as true loop: Body if (Test) goto loop do Body while (Test); { Statement1; Statement2; … Statementn; }

  16. Carnegie Mellon “While” Loop Example • Is this code equivalent to the do-while version? • Must jump out of loop if test fails C Code Goto Version (if (…) goto …) int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } int pcount_do(unsigned x){ int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; }

  17. Carnegie Mellon General “While” Translation While version while (Test) Body Goto Version Do-While Version if (!Test) goto done; loop: Body if (Test) goto loop; done: if (!Test) goto done; do Body while(Test); done:

  18. Carnegie Mellon “For” Loop Example • Is this code equivalent to other versions? C Code #define WSIZE sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; }

  19. Carnegie Mellon “For” Loop Form Init i = 0 General Form Test for (Init; Test; Update ) Body i < WSIZE Update i++ for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } Body { unsigned mask = 1 << i; result += (x & mask) != 0; }

  20. Carnegie Mellon “For” Loop  While Loop For Version for (Init; Test; Update) Body While Version Init; while (Test ) { Body Update; }

  21. Carnegie Mellon “For” Loop  …  Goto Init; if (!Test) goto done; loop: Body Update if (Test) goto loop; done: For Version for (Init; Test; Update) Body While Version Init; while (Test ) { Body Update; } Init; if (!Test) goto done; do Body Update while(Test); done:

  22. Carnegie Mellon “For” Loop Conversion Example Goto Version C Code int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result; } #define WSIZE sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } Init !Test Body Update Test

  23. Example 2 .section .data prompt_str1: .ascii "Enter first number: " str1_end: .set STR1_SIZE, str1_end-prompt_str1 prompt_str2: .ascii "\nThe number entered is : " str2_end: .set STR2_SIZE, str2_end-prompt_str2 .section .bss .lcomm input1 1 .section .text .globl _start _start: movl $4, %eax movl $1, %ebx movl $prompt_str1, %ecx movl $STR1_SIZE, %edx int $0x80 movl $3, %eax movl $0, %ebx movl $input1, %ecx movl $2, %edx int $0x80 movl $4, %eax movl $1, %ebx movl $prompt_str2, %ecx movl $STR2_SIZE, %edx int $0x80 movl $4, %eax movl $1, %ebx movl $input1, %ecx movl $2, %edx int $0x80 exit: movl$1, %eax movl$0, %ebx int$0x80 Introduction

More Related