#====================================================================== # power.s # Control Transfer Instructions #====================================================================== #====================================================================== .section ".rodata" #====================================================================== pcPrompt1: .asciz "Enter the base: " pcPrompt2: .asciz "Enter the exponent: " pcScanfFormat: .asciz "%d" pcResult: .asciz "%d raised to the %d power is %d.\n" #====================================================================== .section ".data" #====================================================================== piPower: .long 1 #====================================================================== .section ".bss" #====================================================================== piBase: .skip 4 piExp: .skip 4 piIndex: .skip 4 #====================================================================== .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 #---------------------------------------------------------------------- .globl main .type main,@function main: pushl %ebp movl %esp, %ebp # printf("Enter the base: "); pushl $pcPrompt1 call printf addl $4, %esp # scanf("%d", &iBase); pushl $piBase pushl $pcScanfFormat call scanf addl $8, %esp # printf("Enter the exponent: "); pushl $pcPrompt2 call printf addl $4, %esp # scanf("%d", &iExp); pushl $piExp pushl $pcScanfFormat call scanf addl $8, %esp # iIndex = 1; movl $1, piIndex loop1: # if (iIndex > iExp) goto loopend1; movl piIndex, %eax cmpl piExp, %eax jg loopend1 # iPower *= iBase; movl piPower, %eax imull piBase movl %eax, piPower # iIndex++; incl piIndex # goto loop1; jmp loop1 loopend1: # printf("%d to the %d power is %d.\n", iBase, iExp, iPower); pushl piPower pushl piExp pushl piBase pushl $pcResult call printf addl $16, %esp # return 0; movl $0, %eax movl %ebp, %esp popl %ebp ret