//---------------------------------------------------------------------- // euclidglobal.s // Author: Bob Dondero //---------------------------------------------------------------------- .section .rodata promptStr: .string "Enter an integer: " scanfFormatStr: .string "%ld" printfFormatStr: .string "The gcd is %ld\n" //---------------------------------------------------------------------- .section .data //---------------------------------------------------------------------- .section .bss l1: .skip 8 l2: .skip 8 lGcd: .skip 8 lTemp: .skip 8 lAbsFirst: .skip 8 lAbsSecond: .skip 8 //---------------------------------------------------------------------- .section .text //-------------------------------------------------------------- // Assign to lGcd the greatest common divisor of l1 and l2. // void gcd(void) //-------------------------------------------------------------- // Must be a multiple of 16 .equ GCD_STACK_BYTECOUNT, 16 gcd: // Prolog sub sp, sp, GCD_STACK_BYTECOUNT str x30, [sp] // lAbsFirst = labs(l1) adr x0, l1 ldr x0, [x0] bl labs adr x1, lAbsFirst str x0, [x1] // lAbsSecond = labs(l1) adr x0, l2 ldr x0, [x0] bl labs adr x1, lAbsSecond str x0, [x1] gcdLoop: // if (lAbsSecond == 0) goto gcdLoopEnd adr x0, lAbsSecond ldr x0, [x0] cmp x0, 0 beq gcdLoopEnd // lTemp = lAbsFirst % lAbsSecond // remainder = (dividend - (quotient * divisor)) adr x0, lAbsFirst ldr x0, [x0] adr x1, lAbsSecond ldr x1, [x1] sdiv x2, x0, x1 mul x3, x2, x1 sub x4, x0, x3 adr x0, lTemp str x4, [x0] // lAbsFirst = lAbsSecond adr x0, lAbsSecond ldr x0, [x0] adr x1, lAbsFirst str x0, [x1] // lAbsSecond = lTemp adr x0, lTemp ldr x0, [x0] adr x1, lAbsSecond str x0, [x1] // goto gcdLoop b gcdLoop gcdLoopEnd: // lGcd = lAbsFirst adr x0, lAbsFirst ldr x0, [x0] adr x1, lGcd str x0, [x1] // Epilog and return ldr x30, [sp] add sp, sp, GCD_STACK_BYTECOUNT ret .size gcd, (. - gcd) //-------------------------------------------------------------- // Read two integers from stdin. Compute their greatest common // divisor, and write it to stdout. Return 0. // int main(void) //-------------------------------------------------------------- // Must be a multiple of 16 .equ MAIN_STACK_BYTECOUNT, 16 .global main main: // Prolog sub sp, sp, MAIN_STACK_BYTECOUNT str x30, [sp] // printf("Enter an integer: ") adr x0, promptStr bl printf // scanf("%ld", &l1) adr x0, scanfFormatStr adr x1, l1 bl scanf // printf("Enter an integer: ") adr x0, promptStr bl printf // scanf("%ld", &l2) adr x0, scanfFormatStr adr x1, l2 bl scanf // gcd() bl gcd // printf("The gcd is %ld\n", lGcd) adr x0, printfFormatStr adr x1, lGcd ldr x1, [x1] bl printf // Epilog and return 0 mov w0, 0 ldr x30, [sp] add sp, sp, MAIN_STACK_BYTECOUNT ret .size main, (. - main)