# # Computing area of trapezoid. # area = h/2(b1 + b2). # Order of provided value is # is h, b1, b2. # Try: 4, 6, 12 --> area 36 # # This example saves and restores "caller save" and "callee save" # registers. Notice that this is not necessary for this code, # but we are providing it as example that illustrates how to save # them. During the implementation of certain functions (e.g., # recursive functions) keeping track of which registers you # are using could be difficult. Following a "caller save"/"callee save" # protocol can help you. # # Notice this example show the kind of comments we are expecting # in your projects. # main: irmovl $0x1000, %esp # init stack ptr rdint %ebx # reading height rdint %esi # reading b1 rdint %edi # reading b2 pushl %eax # caller save registers pushl %ecx pushl %edx pushl %edi # pushing parameter (rightmost first) pushl %esi # pushing parameter pushl %ebx # pushing parameter call area # function call wrint %eax # accessing the return value popl %ebx # removing parameters popl %esi # removing parameters popl %edi # removing parameters popl %edx # restoring caller saved registers popl %ecx popl %eax irmovl 0x0a, %edx # printing newline wrch %edx halt area: pushl %ebp # ON_ENTRY saving old base/frame ptr rrmovl %esp, %ebp # ON_ENTRY set new frame ptr irmovl $8, %eax # We will have two local variables subl %eax, %esp # (8 bytes, 4 byte each). Subtracting # to adjust stack pushl %ebx # callee save registers pushl %esi pushl %edi mrmovl 8(%ebp), %eax # Retrieving height mrmovl 12(%ebp), %edx # Retrieving b1 mrmovl 16(%ebp), %ecx # Retrieving b2 addl %edx, %ecx # adding b1 + b2 rmmovl %ecx, -4(%ebp) # storing sum in local var irmovl $2, %ecx # dividing height by 2 divl %ecx, %eax rmmovl %eax, -8(%ebp) # storing division result in second local var mrmovl -4(%ebp), %edx # accessing local var1 mrmovl -8(%ebp), %eax # accessing local var2 multl %edx, %eax # computes area and leaves result in %eax popl %edi # restoring callee save registers popl %esi popl %ebx rrmovl %ebp, %esp # ON_EXIT reset stack ptr (throws away frame) popl %ebp # ON_EXIT restore old base/frame ptr ret # ON_EXIT