cs313-2006-t2-problemset3-solution

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/09/2011 for the course CPSC 344 taught by Professor Karen during the Fall '10 term at The University of British Columbia.

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 Right Arrow Icon
Ask a homework question - tutors are online