### -------------------------------------------------------------------- ### powerstack.s ### Author: Bob Dondero ### -------------------------------------------------------------------- .section ".rodata" cPrompt1: .string "Enter the base: " cPrompt2: .string "Enter the exponent: " cScanfFormat: .string "%d" cResult: .string "%d to the %d power is %d.\n" ### -------------------------------------------------------------------- .section ".data" ### -------------------------------------------------------------------- .section ".bss" ### -------------------------------------------------------------------- .section ".text" ## ------------------------------------------------------------- ## Read a non-negative base and exponent from stdin. Write ## base raised to the exponent power to stdout. Return 0. ## int main(void) ## ------------------------------------------------------------- ## Local variable offsets: .equ IBASE, -4 .equ IEXP, -8 .equ IPOWER, -12 .equ IINDEX, -16 .globl main .type main,@function main: pushl %ebp movl %esp, %ebp ## int iBase subl $4, %esp ## int iExp subl $4, %esp ## int iPower = 1 pushl $1 ## int iIndex subl $4, %esp ## printf("Enter the base: ") pushl $cPrompt1 call printf addl $4, %esp ## scanf("%d", &iBase) leal IBASE(%ebp), %eax ## Alternative to leal: ## movl %ebp,%eax ## addl $IBASE,%eax pushl %eax pushl $cScanfFormat call scanf addl $8, %esp ## printf("Enter the exponent: ") pushl $cPrompt2 call printf addl $4, %esp ## scanf("%d", &iExp) leal IEXP(%ebp), %eax ## Alternative to leal: ## movl %ebp,%eax ## addl $IEXP,%eax pushl %eax pushl $cScanfFormat call scanf addl $8, %esp ## iIndex = 1 movl $1, IINDEX(%ebp) loop1: ## if (iIndex > iExp) goto loopend1 movl IINDEX(%ebp), %eax cmpl IEXP(%ebp), %eax jg loopend1 ## iPower *= iBase movl IPOWER(%ebp), %eax imull IBASE(%ebp) movl %eax, IPOWER(%ebp) ## iIndex++ incl IINDEX(%ebp) ## goto loop1 jmp loop1 loopend1: ## printf("%d to the %d power is %d.\n", iBase, iExp, iPower) pushl IPOWER(%ebp) pushl IEXP(%ebp) pushl IBASE(%ebp) pushl $cResult call printf addl $16, %esp ## return 0 movl $0, %eax movl %ebp, %esp popl %ebp ret