Can someone please explain how to convert the following C++ code to pep/8 assemb
ID: 3718900 • Letter: C
Question
Can someone please explain how to convert the following C++ code to pep/8 assembly language? Having a hard time. The code changes any lowercase letter to the next letter in the alphabet capitalized. If it's a non letter, it notifies the user.
#include <iostream>
#include<stdlib.h>
#include <ctype.h>
using namespace std;
//function declaration
char Convert_uppercase (char l)
{
//convert lowercase to uppercase
if ((l >= 'a') && (l <= 'z'))
{
//return the charater
return l - 'a' + 'A';
}
}
int main()
{
//declare variables
char lowercase,uppercase;
cout<<"Enter Lower case character:";
//read user input character
cin>>lowercase;
//check if it is character 'z'
if(lowercase=='z')
cout<<"Converted to: "<<'A' <<endl;
// check input is number or character
else if((isalpha(lowercase))&&(lowercase!='z'))
{
//call conversion function
uppercase=Convert_uppercase (lowercase);
//Display the upper case
cout<<"Uppercase:"<<uppercase<<endl;
//Display next character
cout<<"Incremented Character:"<<static_cast<char>(uppercase + 1)<<endl;
}
else
{
//display not a character
cout<<"NOT a character:"<<lowercase<<endl;
}
system("pause");
return 0;
}
Explanation / Answer
We can easily convert the C++ code into assembly language in Ubuntu.
The steps are:
1. open gedit window (Applications ? Accessories ? Text Editor)
2. copy your C++ code into the window and save it. for example, I have saved your c++ code as new.cpp which will appear in document folder.
3. open terminal. (Applications ? Accessories ? Terminal)
4. type in terminal as:
This will create a file named "new.s" in the document folder which will contain the corresponding assembly language code.
********************************************************************************************************
Here is the assembly language code:
.file "new.cpp"
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.text
.globl _Z17Convert_uppercasec
.type _Z17Convert_uppercasec, @function
_Z17Convert_uppercasec:
.LFB971:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, %eax
movb %al, -4(%rbp)
cmpb $96, -4(%rbp)
jle .L2
cmpb $122, -4(%rbp)
jg .L2
movzbl -4(%rbp), %eax
subl $32, %eax
jmp .L1
.L2:
.L1:
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE971:
.size _Z17Convert_uppercasec, .-_Z17Convert_uppercasec
.section .rodata
.LC0:
.string "Enter Lower case character:"
.LC1:
.string "Converted to: "
.LC2:
.string "Uppercase:"
.LC3:
.string "Incremented Character:"
.LC4:
.string "NOT a character:"
.LC5:
.string "pause"
.text
.globl main
.type main, @function
main:
.LFB972:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $24, %rsp
.cfi_offset 3, -24
movl $.LC0, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
leaq -18(%rbp), %rax
movq %rax, %rsi
movl $_ZSt3cin, %edi
call _ZStrsIcSt11char_traitsIcEERSt13basic_istreamIT_T0_ES6_RS3_
movzbl -18(%rbp), %eax
cmpb $122, %al
jne .L5
movl $.LC1, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl $65, %esi
movq %rax, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
jmp .L6
.L5:
movzbl -18(%rbp), %eax
movsbl %al, %eax
movl %eax, %edi
call isalpha
testl %eax, %eax
je .L7
movzbl -18(%rbp), %eax
cmpb $122, %al
je .L7
movzbl -18(%rbp), %eax
movsbl %al, %eax
movl %eax, %edi
call _Z17Convert_uppercasec
movb %al, -17(%rbp)
movsbl -17(%rbp), %ebx
movl $.LC2, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl %ebx, %esi
movq %rax, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
movzbl -17(%rbp), %eax
addl $1, %eax
movsbl %al, %ebx
movl $.LC3, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl %ebx, %esi
movq %rax, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
jmp .L6
.L7:
movzbl -18(%rbp), %eax
movsbl %al, %ebx
movl $.LC4, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl %ebx, %esi
movq %rax, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi
movq %rax, %rdi
call _ZNSolsEPFRSoS_E
.L6:
movl $.LC5, %edi
call system
movl $0, %eax
addq $24, %rsp
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE972:
.size main, .-main
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB982:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L9
cmpl $65535, -8(%rbp)
jne .L9
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
call __cxa_atexit
.L9:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE982:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__sub_I__Z17Convert_uppercasec, @function
_GLOBAL__sub_I__Z17Convert_uppercasec:
.LFB983:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE983:
.size _GLOBAL__sub_I__Z17Convert_uppercasec, .-_GLOBAL__sub_I__Z17Convert_uppercasec
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I__Z17Convert_uppercasec
.hidden __dso_handle
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
.section .note.GNU-stack,"",@progbits
**********************************************************************************************************