Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the following code I have written in a file called Lab7.asm: .386 .model

ID: 3804785 • Letter: T

Question

This is the following code I have written in a file called Lab7.asm:

.386

.model flat

public _Square
public _Multiply
public _Divide

.code

_Square proc
       push ebp
       mov ebp, esp
       mov eax, [ebp + 8]
       imul eax
       pop ebp
       ret
_Square endp

_Multiply proc
       push ebp
       mov ebp, esp
       mov eax, [ebp + 8]
       mov ebx, [ebp + 12]
       imul ebx
       pop ebp
       ret
_Multiply endp

_Divide proc
       push ebp
       mov ebp, esp
       mov eax, [ebp + 8]
       mov ebx, [ebp + 12]
       mov ecx, [ebp + 16]
       mov edx, 0
       idiv ebx
       mov ecx, eax
       mov ebx, [ebp + 20]
       mov ebx, edx
       pop ebp
       ret
_Divide endp

end

And this is my C++ program:

#include <iostream>
using namespace std;

enum ResultCode {ShowSquare, ShowMultiply, ShowDivide, ShowRemainder, ShowDivideFailure};
enum SuccessCode {Failure, Success};

extern "C" SuccessCode Divide(long, long, long&, long&);
extern "C" long Multiply(long, long);
extern "C" void PrintResult(ResultCode, long);
extern "C" long Square(long);

void main()
{
   long Num1;
   long Num2;
   long Result;
   long Remainder;
   do
   {
       cout << "Enter Number to Square" << endl;
       cin >> Num1;
       Result = Square(Num1);
       cout << "Square is: " << Result << endl;
       cout << "Enter two numbers to multiply" << endl;
       cin >> Num1 >> Num2;
       Result = Multiply(Num1, Num2);
       cout << "Result of multiply is: " << Result << endl;
       cout << "Enter mumber to divide into then number to divide by" << endl;
       cin >> Num1 >> Num2;
       if (Divide(Num1, Num2, Result, Remainder) == Success)
           cout << "Result is " << Result << " and remainder is " << Remainder << endl;
       else
           cout << "Attempted division by zero";
   } while (Result > 0);
}

void PrintResult(ResultCode PrintCode, long Value)
{
   switch (PrintCode)
   {
   case ShowSquare:
       cout << "Display of square is: " << Value << endl;
       break;
   case ShowMultiply:
       cout << "Display of multiply is: " << Value << endl;
       break;
   case ShowDivide:
       cout << "Display of divide is " << Value << endl;
       break;
   case ShowRemainder:
       cout << "Display of remainder is " << Value << endl;
       break;
   case ShowDivideFailure:
       cout << "Display of Division by zero" << endl;
       break;
   default:
       cout << "Error in assembly routines" << endl;
   }
}

These are the instructions I was given:

Write the three functions (Square, Multiply, and Divide) required for the following C++ program. Square and Multiply will return the result to the calling program and will also call PrintResult to display the answer. If successful, Divide will return a 1, set the value of theresult and the remainder into the appropriate parameters, and call PrintResult twice (once for result and once for remainder). If Divide is not successful, it will return a 0 and call PrintResult to display the failure message. Link them to the above program to demonstrate their functionality.

I am a proficient C++ and Java programmer, but since this is my first semester working with Intel Assembly, things have been a bit weird. I've been fine on my own figuring things out up until this 7th homework lab. I've written the above assembly code by myself, but I'm stuck on what to do next. I tried calling PrintResult with the statement: call _PrintResult as the next step wants me to do, but this only results in the compiler telling me that the function is an undefined symbol. Also, how do I get my didvide function to return a 0 or 1 in the assembly code?

Explanation / Answer

   .file   "c_bRZnnU"
   .section   .ctors,"w"
   .align 4
   .long   __GLOBAL__I_main
   .text
   .align 2
   .p2align 2,,3
   .def   __Z41__static_initialization_and_destruction_0ii;   .scl   3;   .type   32;   .endef
__Z41__static_initialization_and_destruction_0ii:
   pushl   %ebp
   movl   %esp, %ebp
   subl   $8, %esp
   decl   %eax
   je   L7
L5:
   leave
   ret
   .p2align 2,,3
L7:
   cmpl   $65535, %edx
   jne   L5
   subl   $12, %esp
   pushl   $__ZSt8__ioinit
   call   __ZNSt8ios_base4InitC1Ev
   movl   $___tcf_0, (%esp)
   call   _atexit
   addl   $16, %esp
   leave
   ret
   .align 2
   .p2align 2,,3
   .def   __GLOBAL__I_main;   .scl   3;   .type   32;   .endef
__GLOBAL__I_main:
   pushl   %ebp
   movl   %esp, %ebp
   movl   $65535, %edx
   movl   $1, %eax
   leave
   jmp   __Z41__static_initialization_and_destruction_0ii
   .align 2
   .p2align 2,,3
   .def   ___tcf_0;   .scl   3;   .type   32;   .endef
___tcf_0:
   pushl   %ebp
   movl   %esp, %ebp
   subl   $20, %esp
   pushl   $__ZSt8__ioinit
   call   __ZNSt8ios_base4InitD1Ev
   addl   $16, %esp
   leave
   ret
   .section .rdata,"dr"
LC0:
   .ascii "Display of square is: "
LC1:
   .ascii "Display of multiply is: "
LC2:
   .ascii "Display of divide is "
LC3:
   .ascii "Display of remainder is "
LC4:
   .ascii "Display of Division by zero"
LC5:
   .ascii "Error in assembly routines"
   .text
   .align 2
   .p2align 2,,3
.globl _PrintResult
   .def   _PrintResult;   .scl   2;   .type   32;   .endef
_PrintResult:
   pushl   %ebp
   movl   %esp, %ebp
   pushl   %ebx
   subl   $4, %esp
   movl   8(%ebp), %eax
   movl   12(%ebp), %ebx
   cmpl   $4, %eax
   jbe   L24
   pushl   %eax
   pushl   $26
   pushl   $LC5
L23:
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   addl   $16, %esp
   movl   $__ZSt4cout, 8(%ebp)
   movl   -4(%ebp), %ebx
   leave
   jmp   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
   .p2align 2,,3
L24:
   jmp   *L19(,%eax,4)
   .section .rdata,"dr"
   .align 4
L19:
   .long   L14
   .long   L15
   .long   L16
   .long   L17
   .long   L18
   .text
L18:
   pushl   %edx
   pushl   $27
   pushl   $LC4
   jmp   L23
L14:
   pushl   %eax
   pushl   $22
   pushl   $LC0
   .p2align 2,,3
L22:
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   popl   %eax
   popl   %edx
   pushl   %ebx
   pushl   $__ZSt4cout
   call   __ZNSo9_M_insertIlEERSoT_
   addl   $16, %esp
   movl   %eax, 8(%ebp)
   movl   -4(%ebp), %ebx
   leave
   jmp   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
L15:
   pushl   %ecx
   pushl   $24
   pushl   $LC1
   jmp   L22
L16:
   pushl   %eax
   pushl   $21
   pushl   $LC2
   jmp   L22
L17:
   pushl   %ecx
   pushl   $24
   pushl   $LC3
   jmp   L22
   .def   ___main;   .scl   2;   .type   32;   .endef
   .section .rdata,"dr"
LC6:
   .ascii "Enter Number to Square"
LC7:
   .ascii "Square is: "
LC8:
   .ascii "Enter two numbers to multiply"
LC9:
   .ascii "Result of multiply is: "
   .align 4
LC10:
   .ascii "Enter mumber to divide into then number to divide by"
LC11:
   .ascii "Result is "
LC12:
   .ascii " and remainder is "
LC13:
   .ascii "Attempted division by zero"
   .text
   .align 2
   .p2align 2,,3
.globl _main
   .def   _main;   .scl   2;   .type   32;   .endef
_main:
   leal   4(%esp), %ecx
   andl   $-16, %esp
   pushl   -4(%ecx)
   pushl   %ebp
   movl   %esp, %ebp
   pushl   %edi
   pushl   %esi
   pushl   %ebx
   pushl   %ecx
   subl   $24, %esp
   call   ___main
   leal   -20(%ebp), %edi
   jmp   L27
   .p2align 2,,3
L28:
   pushl   %eax
   pushl   $26
   pushl   $LC13
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   addl   $16, %esp
   movl   -28(%ebp), %eax
   testl   %eax, %eax
   jle   L35
L27:
   pushl   %ecx
   pushl   $22
   pushl   $LC6
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   movl   $__ZSt4cout, (%esp)
   call   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
   popl   %eax
   popl   %edx
   pushl   %edi
   pushl   $__ZSt3cin
   call   __ZNSi10_M_extractIlEERSiRT_
   popl   %eax
   pushl   -20(%ebp)
   call   _Square
   movl   %eax, %ebx
   movl   %eax, -28(%ebp)
   addl   $12, %esp
   pushl   $11
   pushl   $LC7
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   popl   %esi
   popl   %eax
   pushl   %ebx
   pushl   $__ZSt4cout
   call   __ZNSo9_M_insertIlEERSoT_
   movl   %eax, (%esp)
   call   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
   addl   $12, %esp
   pushl   $29
   pushl   $LC8
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   movl   $__ZSt4cout, (%esp)
   call   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
   popl   %ecx
   popl   %ebx
   pushl   %edi
   pushl   $__ZSt3cin
   call   __ZNSi10_M_extractIlEERSiRT_
   popl   %esi
   popl   %edx
   leal   -24(%ebp), %edx
   pushl   %edx
   pushl   %eax
   call   __ZNSi10_M_extractIlEERSiRT_
   popl   %ecx
   popl   %ebx
   pushl   -24(%ebp)
   pushl   -20(%ebp)
   call   _Multiply
   movl   %eax, %ebx
   movl   %eax, -28(%ebp)
   addl   $12, %esp
   pushl   $23
   pushl   $LC9
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   popl   %eax
   popl   %edx
   pushl   %ebx
   pushl   $__ZSt4cout
   call   __ZNSo9_M_insertIlEERSoT_
   movl   %eax, (%esp)
   call   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
   addl   $12, %esp
   pushl   $52
   pushl   $LC10
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   movl   $__ZSt4cout, (%esp)
   call   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
   popl   %ebx
   popl   %esi
   pushl   %edi
   pushl   $__ZSt3cin
   call   __ZNSi10_M_extractIlEERSiRT_
   popl   %edx
   popl   %ecx
   leal   -24(%ebp), %edx
   pushl   %edx
   pushl   %eax
   call   __ZNSi10_M_extractIlEERSiRT_
   addl   $16, %esp
   leal   -32(%ebp), %eax
   pushl   %eax
   leal   -28(%ebp), %edx
   pushl   %edx
   pushl   -24(%ebp)
   pushl   -20(%ebp)
   call   _Divide
   addl   $16, %esp
   decl   %eax
   jne   L28
   movl   -32(%ebp), %esi
   movl   -28(%ebp), %ebx
   pushl   %eax
   pushl   $10
   pushl   $LC11
   pushl   $__ZSt4cout
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   popl   %ecx
   popl   %eax
   pushl   %ebx
   pushl   $__ZSt4cout
   call   __ZNSo9_M_insertIlEERSoT_
   movl   %eax, %ebx
   addl   $12, %esp
   pushl   $18
   pushl   $LC12
   pushl   %eax
   call   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i
   popl   %eax
   popl   %edx
   pushl   %esi
   pushl   %ebx
   call   __ZNSo9_M_insertIlEERSoT_
   movl   %eax, (%esp)
   call   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
   addl   $16, %esp
   movl   -28(%ebp), %eax
   testl   %eax, %eax
   jg   L27
   .p2align 2,,3
L35:
   xorl   %eax, %eax
   leal   -16(%ebp), %esp
   popl   %ecx
   popl   %ebx
   popl   %esi
   popl   %edi
   leave
   leal   -4(%ecx), %esp
   ret
.lcomm __ZSt8__ioinit,16
   .def   __ZNSt8ios_base4InitC1Ev;   .scl   2;   .type   32;   .endef
   .def   __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_;   .scl   2;   .type   32;   .endef
   .def   _Square;   .scl   2;   .type   32;   .endef
   .def   _Multiply;   .scl   2;   .type   32;   .endef
   .def   _Divide;   .scl   2;   .type   32;   .endef
   .def   __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i;   .scl   2;   .type   32;   .endef
   .def   __ZNSi10_M_extractIlEERSiRT_;   .scl   2;   .type   32;   .endef
   .def   __ZNSo9_M_insertIlEERSoT_;   .scl   2;   .type   32;   .endef
   .def   __ZNSt8ios_base4InitD1Ev;   .scl   2;   .type   32;   .endef
   .def   _atexit;   .scl   2;   .type   32;   .endef

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote