## ============================================================ ## powerstack.s ## The Stack ## ============================================================ ## ============================================================ .section ".rodata" ## ============================================================ cPrompt1: .asciz "Enter the base: " cPrompt2: .asciz "Enter the exponent: " cScanfFormat: .asciz "%d" cResult: .asciz "%d raised to the %d power is %d.\n" ## ============================================================ .section ".data" ## ============================================================ ## ============================================================ .section ".bss" ## ============================================================ ## ============================================================ .section ".text" ## ============================================================ ## ------------------------------------------------------------ ## int main(int argc, char *argv[]) ## ## Read a non-negative base and exponent from stdin. Write ## base raised to the exponent power to stdout. ## ## Formal parameter offsets: .equ ARGC, 8 .equ ARGV, 12 ## 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 # Or: 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 # Or: 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