### ------------------------------------------------------------------ ### 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 iPerimeter: .skip 4 ### ------------------------------------------------------------------ .section ".text" ## ----------------------------------------------------------- ## int main(int argc, char *argv[]) ## Read a rectangles length and width from stdin, and write ## its perimeter to stdout. ## ----------------------------------------------------------- ## Formal parameter offsets: .equ ARGC, 8 .equ ARGV, 12 .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, iPerimeter ## printf("The rectangle's perimeter is %d.\n", iPerimeter); pushl iPerimeter pushl $cResult call printf addl $8, %esp ## return 0; movl $0, %eax movl %ebp, %esp popl %ebp ret