not sure how to fix this.

Okay so basically we have to take a an integer and print out its hex value. From what I was able to gather this was my result however I get an error and I am not sure how to fix it. Our teacher wanted in assembly language and I am very new to this so I am not sure if this is right. Anyways here is the code and error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;

}
std::string itoa(int n)
{
   const int max_size = std::numeric_limits<int>::digits10 + 1 /*sign*/ + 1 /*0-terminator*/;
   char buffer[max_size] = {0};
   sprintf(buffer, "%d", n);
   return std::string(buffer);
}





|19|error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
closed account (Dy7SLyTq)
how are you compiling?
@OP

I have to say that code is a stunning mixture of C and C++, with some major conceptual problems.

Our teacher wanted in assembly language....


So how were you planning to go from this code to assembly? Especially the C++ part? Please tell me you weren't going to get the compiler to produce the assembly?

..... and I am very new to this so I am not sure if this is right.


Teachers provide enough info for students to do the assignments, so if you don't know what you are doing to this extent, what might be the reason for that?

Don't name one of your own functions the same as those found in a library, that you include a header for. Normally one would declare the functions before main, and put the definitions after main. Apparently you didn't get a compilation error for not doing this because itoa is in <stdlib.h>

The main() function is C code, and you provide the right number of arguments for stdlib itoa. Although you didn't check the return value of the scanf, so presumably you are going to pray that it works?

Then, BAM , you define your own itoa function with C++, but it only takes 1 argument !!

As DTSCode alluded to, if you compiled with a C compiler, it falls over because it doesn't recognise C++.

Even if you used a C++ compiler there are still problems: printf does not handle std::strings.

So a major re think needed - I would advise actually going to your Lectures & Tutorials, and realise when they ask for assembly code - then do that, don't write in another language.

EDIT: There are several other problems I didn't bother to mention.
Last edited on
Thank you TheIdeasMan. I do actually go to my teachers lectures. I figured what I was doing wasn't right and that is why I was here asking. I will rewrite my code and come back with something new and I'll make sure its proper this time.
So here is the new code it works but I was wondering if there is a simpler way of doing it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Stack frame
    .equ anInt,-4
    .equ localSize,-16
# Read only data
    .section .rodata
prompt:
    .string "Enter an integer number: "
scanFormat:
    .string "%i"
printFormat:
    .string "%i = %x\n"
# Code
    .text # switch to text segment
    .globl main
    .type main, @function
main:
 pushq  %rbp                # save caller’s base pointer
 movq   %rsp, %rbp          # establish our base pointer
 addq   $localSize, %rsp    # for local variable

 movl   $prompt, %edi       # address of prompt text string
 movl   $0, %eax            # no floating point args.
 call   printf              # invoke printf function

 leaq   anInt(%rbp), %rsi   # place to store integer
 movl   $scanFormat, %edi   # address of scanf format string
 movl   $0, %eax            # no floating point args.
 call   scanf               # invoke scanf function

 movl   anInt(%rbp), %edx   # the integer
 movl   anInt(%rbp), %esi   # two copies
 movl   $printFormat, %edi  # address of printf text string
 movl   $0, %eax            # no floating point args.
 call   printf              # invoke printf function

 movl   $0, %eax            # return 0
 movq   %rbp, %rsp          # delete local variables
 popq   %rbp                # restore caller’s base pointer
 ret                        # back to calling function
Topic archived. No new replies allowed.