{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs313-2006-t2-problemset3-solution

# cs313-2006-t2-problemset3-solution - CPSC 313 06W Term 2...

This preview shows pages 1–5. Sign up to view the full content.

CPSC 313 06W Term 2 Problem Set #3 - Solution 1. (a) int gcd(int a, int b) { if (a == b) return a; else if (a > b) return gcd(a - b, b); else return gcd(a, b - a); } .file "gcdrec.c" .text .p2align 4,,15 .globl gcd .type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jne .L9 jmp .L3 .p2align 4,,7 .L11: subl %eax, %edx cmpl %eax, %edx je .L3 .L9: cmpl %eax, %edx .p2align 4,,5 jg .L11 subl %edx, %eax cmpl %eax, %edx jne .L9 .L3: popl %ebp .p2align 4,,4 ret .size gcd, .-gcd .ident "GCC: (GNU) 4.1.0 (SUSE Linux)" .section .note.GNU-stack,"",@progbits 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
int gcd(int a, int b) { for (; a != b; a > b ? (a -= b) : (b -= a)) ; return a; } .file "gcdfor.c" .text .p2align 4,,15 .globl gcd .type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %edx cmpl %eax, %edx jne .L8 jmp .L2 .p2align 4,,7 .L11: subl %edx, %eax cmpl %edx, %eax je .L2 .L8: cmpl %edx, %eax .p2align 4,,5 jg .L11 subl %eax, %edx cmpl %edx, %eax jne .L8 .L2: popl %ebp .p2align 4,,4 ret .size gcd, .-gcd .ident "GCC: (GNU) 4.1.0 (SUSE Linux)" .section .note.GNU-stack,"",@progbits 2
int gcd(int a, int b) { while (a != b) { if (a > b) { a -= b; } else { b -= a; } } return a; } .file "gcdwhile.c" .text .p2align 4,,15 .globl gcd .type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %edx cmpl %eax, %edx jne .L8 jmp .L2 .p2align 4,,7 .L11: subl %edx, %eax cmpl %edx, %eax je .L2 .L8: cmpl %edx, %eax .p2align 4,,5 jg .L11 subl %eax, %edx cmpl %edx, %eax jne .L8 .L2: popl %ebp .p2align 4,,4 ret .size gcd, .-gcd .ident "GCC: (GNU) 4.1.0 (SUSE Linux)" .section .note.GNU-stack,"",@progbits 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
int gcd(int a, int b) { do { if (a > b) { a -= b; } else if (b > a) { b -= a; } } while (a != b); return a; } .file "gcddo.c" .text .p2align 4,,15 .globl gcd .type gcd, @function gcd: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl 12(%ebp), %edx jmp .L3 .p2align 4,,7 .L14: subl %edx, %eax .L6: cmpl %edx, %eax je .L13 .L3: cmpl %edx, %eax jg .L14 .p2align 4,,5 jge .L6 subl %eax, %edx cmpl %edx, %eax .p2align 4,,5 jne .L3 .p2align 4,,7 .L13: popl %ebp .p2align 4,,6 ret .size gcd, .-gcd
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

cs313-2006-t2-problemset3-solution - CPSC 313 06W Term 2...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online