### -------------------------------------------------------------------- ### rect.s ### Author: Bob Dondero ### Arithmetic and Shift Instructions ### -------------------------------------------------------------------- .section ".rodata" cLengthPrompt: .asciz "Rectangle length: " cWidthPrompt: .asciz "Rectangle width: " cScanfFormat: .asciz "%d" cResult: .asciz "The rectangle's perimeter is %d.\n" ### -------------------------------------------------------------------- .section ".data" ### -------------------------------------------------------------------- .section ".bss" iLength: .skip 4 iWidth: .skip 4 iPerim: .skip 4 ### -------------------------------------------------------------------- .section ".text" ## ------------------------------------------------------------- ## int main(void) ## Read a rectangles length and width from stdin, and write ## its perimeter to stdout. Return 0. ## ------------------------------------------------------------- .globl main .type main,@function main: pushl %ebp movl %esp, %ebp ## printf("Rectangle length: ") pushl $cLengthPrompt call printf addl $4, %esp ## scanf("%d", &iLength) pushl $iLength pushl $cScanfFormat call scanf addl $8, %esp ## printf("Rectangle width: ") pushl $cWidthPrompt call printf addl $4, %esp ## scanf("%d", &iWidth) pushl $iWidth pushl $cScanfFormat call scanf addl $8, %esp ## iPerimeter = 2 * (iLength + iWidth) movl iLength, %eax addl iWidth, %eax sall $1, %eax ## Alternative to sall: ## movl $2, %ecx ## imull %ecx movl %eax, iPerim ## printf("The rectangle's perimeter is %d.\n", iPerim) pushl iPerim pushl $cResult call printf addl $8, %esp ## return 0 movl $0, %eax movl %ebp, %esp popl %ebp ret