0

I am using the ARMv8 instruction set with Aarch64 execution state. The problem I am having, is were supposed to translate C code into assembly, and I am stuck on the following:

while (c != '\n' && c != EOF)
    c = getchar();

I know the basic setup for the while loop, as well as how to test c != '\n' but the issue I have is how to write EOF in assembly. Can i just type

cmp c_r, EOF
b.eq skipwhile

or is it something else?

Erick P
  • 87
  • 1
  • 2
  • 9
  • it's a macro in C which most probably expands to -1. Try to find a similar macro or just use -1 – phuclv Mar 30 '17 at 02:05
  • printf("%X\n",EOF); – old_timer Mar 30 '17 at 02:21
  • and then figure it out from there. – old_timer Mar 30 '17 at 02:39
  • 1
    FYI: You can use `gcc -E infile.c` to preprocess-only your source code. This will replace all macros, include files, etc. There will be a LOT of empty lines, but you'll be seeing what the actual compiler sees, and so macros like EOF will get replaced by, e.g., `(-1)` or something. This can be very useful when you are compiling by hand. (There are similar options on most compilers, if you aren't using `gcc`.) (There are also options to generate assembly, just sayin'... ;-) – aghast Mar 30 '17 at 04:07

1 Answers1

0

It depends on what code you're using in assembly for getchar() -

  • If you're just using a call to the assembly version of standard C library getchar(), then you can test with the C library's definition of EOF - normally -1.
  • If you're implementing your own getchar() in assembly, then you need to match your test to the return value of your getchar() implementation when it gets to the end-of-file.
iVoid
  • 701
  • 3
  • 14