32-bit Calculator Converting from C++ to HLA

Lab 4

Introduction:

bit calculator similar to the one created in Lab 1, 2 and 3.  The calculator will display a menu asking the user what operation to perform.  The operations supported are Addition, Multiplication and Exponent.  Once the user selects an operation, the program will ask the user for the values and perform the operation. For this lab were told to use C or C++ to write the code and then later it to assembly, in order to do that we had use  the following commandS programName.c.  We cloud use any functions to perform the all the operations.  we can use if, while, for, or related commands.  Once the program computers the value and displays the result, it should return to the original menu until the user chooses to exit. We had to use “g++ lab42.cpp -o lab 42” command to convert from C++ to HLA.

Some of the observations that I have notice were that in Assembly that to establish a source and destination , HLA the command is written as mov(source, dest). HLA and C++ resembled the most compared to each other as the most of the code in HLA is compatible ,in HLA but the code just has to be tweaked a little bit then C++. I noticed that all the codes had different headings before each code was started. In HLA, all the variables used had declared as a static variable. In C++ you have the ability to declare a static variable but it is not necessary to do to have the variables to be in your code. With HLA . Data is always being moved in and out of registers. In C, you do not deal with moving data but put more concentration on displaying output or creating a solution to a problem in a particular way.

We did had little problem with converting from C++ to HLA becasue we were using a wrong command. we were using gcc where we should be using g++ for the converting part.

My partner is Fredrick Jah

C++ code

#include//standard header
#include //header for exit command
using namespace std;

int options();//declaring loop
int power(int a1, int b1)//power command
{
int c1 = 1;
while(b1--)
c1 *= a1;

return c1;
}

int main()//execute loop
{
options();//loop
}
int options()//loop to run options 1 to 4 continously

{

int num=1;//declaring number
int factorial=1;//declaring factorial
int add1 = 0;//declaring add1
int add2 = 0;//declaring add2
int choice;//declaring choice

cout<<"Enter 1 for sum of a number"<<endl;//display option1
cout<<"Enter 2 for factorial of a number"<<endl;//display option 2
cout<<"Enter 3 for 2**n"<<endl;//display option 3
cout<<"Enter 4 to exit"<<endl;//display option 4 cin>>choice;//get choice

if(choice==1)
{

cout<<"Enter Number To Find Its Sum: ";//display sum cin>>add1;

for(int b=0;b<=add1;b++)//standard loop

{

add2=add2+b;//add factorial

}

cout<<"The Sum of the number is = "<<add2<<endl;

}

if (choice==2)
{

cout<<"Enter Number To Find its Factorial: "; cin>>num;

for(int a=1;a<=num;a++)//standard loop

{

factorial=factorial*a;//multiply factorial

}

cout<<"Factorial of Given Number is = "<<factorial<<endl;

}

if (choice==3)
{
int n=0;
cout<< "Enter value for n"<< endl; cin>>n;
cout << power(2, n) << endl;//2^n
}

if(choice==4)
{
exit(1);//exit program
cout<<"End of program"<<endl;

}

while(choice!=4)//goes back to menu after choices 1-3
{
options();
}

}

 

HLA code:

.file    "lab42.cpp"
    .local    _ZStL8__ioinit
    .comm    _ZStL8__ioinit,1,1
    .text
    .globl    _Z5powerii
    .type    _Z5powerii, @function
_Z5powerii:
.LFB970:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $16, %esp
    movl    $1, -4(%ebp)
    jmp    .L2
.L3:
    movl    -4(%ebp), %eax
    imull    8(%ebp), %eax
    movl    %eax, -4(%ebp)
.L2:
    cmpl    $0, 12(%ebp)
    setne    %al
    subl    $1, 12(%ebp)
    testb    %al, %al
    jne    .L3
    movl    -4(%ebp), %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE970:
    .size    _Z5powerii, .-_Z5powerii
    .globl    main
    .type    main, @function
main:
.LFB971:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    call    _Z7optionsv
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE971:
    .size    main, .-main
    .section    .rodata
.LC0:
    .string    "Enter 1 for sum of a number"
    .align 4
.LC1:
    .string    "Enter 2 for factorial of a number"
.LC2:
    .string    "Enter 3 for 2**n"
.LC3:
    .string    "Enter 4 to exit"
    .align 4
.LC4:
    .string    "Enter Number To Find Its Sum:   "
.LC5:
    .string    "The Sum of the number is = "
    .align 4
.LC6:
    .string    "Enter Number To Find its Factorial:   "
    .align 4
.LC7:
    .string    "Factorial of Given Number is = "
.LC8:
    .string    "Enter value for n"
    .text
    .globl    _Z7optionsv
    .type    _Z7optionsv, @function
_Z7optionsv:
.LFB972:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $56, %esp
    movl    $1, -40(%ebp)
    movl    $1, -24(%ebp)
    movl    $0, -36(%ebp)
    movl    $0, -20(%ebp)
    movl    $.LC0, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
    movl    $.LC1, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
    movl    $.LC2, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
    movl    $.LC3, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
    leal    -32(%ebp), %eax
    movl    %eax, 4(%esp)
    movl    $_ZSt3cin, (%esp)
    call    _ZNSirsERi
    movl    -32(%ebp), %eax
    cmpl    $1, %eax
    jne    .L6
    movl    $.LC4, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    leal    -36(%ebp), %eax
    movl    %eax, 4(%esp)
    movl    $_ZSt3cin, (%esp)
    call    _ZNSirsERi
    movl    $0, -16(%ebp)
    jmp    .L7
.L8:
    movl    -16(%ebp), %eax
    addl    %eax, -20(%ebp)
    addl    $1, -16(%ebp)
.L7:
    movl    -36(%ebp), %eax
    cmpl    %eax, -16(%ebp)
    setle    %al
    testb    %al, %al
    jne    .L8
    movl    $.LC5, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movl    -20(%ebp), %edx
    movl    %edx, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEi
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
.L6:
    movl    -32(%ebp), %eax
    cmpl    $2, %eax
    jne    .L9
    movl    $.LC6, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    leal    -40(%ebp), %eax
    movl    %eax, 4(%esp)
    movl    $_ZSt3cin, (%esp)
    call    _ZNSirsERi
    movl    $1, -12(%ebp)
    jmp    .L10
.L11:
    movl    -24(%ebp), %eax
    imull    -12(%ebp), %eax
    movl    %eax, -24(%ebp)
    addl    $1, -12(%ebp)
.L10:
    movl    -40(%ebp), %eax
    cmpl    %eax, -12(%ebp)
    setle    %al
    testb    %al, %al
    jne    .L11
    movl    $.LC7, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movl    -24(%ebp), %edx
    movl    %edx, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEi
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
.L9:
    movl    -32(%ebp), %eax
    cmpl    $3, %eax
    jne    .L12
    movl    $0, -28(%ebp)
    movl    $.LC8, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
    leal    -28(%ebp), %eax
    movl    %eax, 4(%esp)
    movl    $_ZSt3cin, (%esp)
    call    _ZNSirsERi
    movl    -28(%ebp), %eax
    movl    %eax, 4(%esp)
    movl    $2, (%esp)
    call    _Z5powerii
    movl    %eax, 4(%esp)
    movl    $_ZSt4cout, (%esp)
    call    _ZNSolsEi
    movl    $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
    movl    %eax, (%esp)
    call    _ZNSolsEPFRSoS_E
.L12:
    movl    -32(%ebp), %eax
    cmpl    $4, %eax
    jne    .L14
    movl    $1, (%esp)
    call    exit
.L15:
    call    _Z7optionsv
.L14:
    movl    -32(%ebp), %eax
    cmpl    $4, %eax
    setne    %al
    testb    %al, %al
    jne    .L15
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE972:
    .size    _Z7optionsv, .-_Z7optionsv
    .type    _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB981:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $24, %esp
    cmpl    $1, 8(%ebp)
    jne    .L16
    cmpl    $65535, 12(%ebp)
    jne    .L16
    movl    $_ZStL8__ioinit, (%esp)
    call    _ZNSt8ios_base4InitC1Ev
    movl    $_ZNSt8ios_base4InitD1Ev, %eax
    movl    $__dso_handle, 8(%esp)
    movl    $_ZStL8__ioinit, 4(%esp)
    movl    %eax, (%esp)
    call    __cxa_atexit
.L16:
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE981:
    .size    _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
    .type    _GLOBAL__sub_I__Z5powerii, @function
_GLOBAL__sub_I__Z5powerii:
.LFB982:
    .cfi_startproc
    pushl    %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    subl    $24, %esp
    movl    $65535, 4(%esp)
    movl    $1, (%esp)
    call    _Z41__static_initialization_and_destruction_0ii
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE982:
    .size    _GLOBAL__sub_I__Z5powerii, .-_GLOBAL__sub_I__Z5powerii
    .section    .ctors,"aw",@progbits
    .align 4
    .long    _GLOBAL__sub_I__Z5powerii
    .weakref    _ZL20__gthrw_pthread_oncePiPFvvE,pthread_once
    .weakref    _ZL27__gthrw_pthread_getspecificj,pthread_getspecific
    .weakref    _ZL27__gthrw_pthread_setspecificjPKv,pthread_setspecific
    .weakref    _ZL22__gthrw_pthread_createPmPK14pthread_attr_tPFPvS3_ES3_,pthread_create
    .weakref    _ZL20__gthrw_pthread_joinmPPv,pthread_join
    .weakref    _ZL21__gthrw_pthread_equalmm,pthread_equal
    .weakref    _ZL20__gthrw_pthread_selfv,pthread_self
    .weakref    _ZL22__gthrw_pthread_detachm,pthread_detach
    .weakref    _ZL22__gthrw_pthread_cancelm,pthread_cancel
    .weakref    _ZL19__gthrw_sched_yieldv,sched_yield
    .weakref    _ZL26__gthrw_pthread_mutex_lockP15pthread_mutex_t,pthread_mutex_lock
    .weakref    _ZL29__gthrw_pthread_mutex_trylockP15pthread_mutex_t,pthread_mutex_trylock
    .weakref    _ZL31__gthrw_pthread_mutex_timedlockP15pthread_mutex_tPK8timespec,pthread_mutex_timedlock
    .weakref    _ZL28__gthrw_pthread_mutex_unlockP15pthread_mutex_t,pthread_mutex_unlock
    .weakref    _ZL26__gthrw_pthread_mutex_initP15pthread_mutex_tPK19pthread_mutexattr_t,pthread_mutex_init
    .weakref    _ZL29__gthrw_pthread_mutex_destroyP15pthread_mutex_t,pthread_mutex_destroy
    .weakref    _ZL30__gthrw_pthread_cond_broadcastP14pthread_cond_t,pthread_cond_broadcast
    .weakref    _ZL27__gthrw_pthread_cond_signalP14pthread_cond_t,pthread_cond_signal
    .weakref    _ZL25__gthrw_pthread_cond_waitP14pthread_cond_tP15pthread_mutex_t,pthread_cond_wait
    .weakref    _ZL30__gthrw_pthread_cond_timedwaitP14pthread_cond_tP15pthread_mutex_tPK8timespec,pthread_cond_timedwait
    .weakref    _ZL28__gthrw_pthread_cond_destroyP14pthread_cond_t,pthread_cond_destroy
    .weakref    _ZL26__gthrw_pthread_key_createPjPFvPvE,pthread_key_create
    .weakref    _ZL26__gthrw_pthread_key_deletej,pthread_key_delete
    .weakref    _ZL30__gthrw_pthread_mutexattr_initP19pthread_mutexattr_t,pthread_mutexattr_init
    .weakref    _ZL33__gthrw_pthread_mutexattr_settypeP19pthread_mutexattr_ti,pthread_mutexattr_settype
    .weakref    _ZL33__gthrw_pthread_mutexattr_destroyP19pthread_mutexattr_t,pthread_mutexattr_destroy
    .ident    "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section    .note.GNU-stack,"",@progbits

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *