Is it possible to run a C-File using an ARM CPU emulator?

0

I had the idea of emulating an ARM CPU to run different programs. To begin with, I wanted emulate the CPU and run for example a simple "hello world" written in C.

Sadly I found no answer using Google etc.

Is it possible to run, for example a C-File using an ARM CPU emulator?

I thought of emulating a CPU without GUI – just using the command-line on which I want to run the C-file.

Tailor

Posted 2018-11-12T13:19:01.247

Reputation: 3

Maybe you can try with a container? – Babblo – 2018-11-12T13:42:49.137

Container like a Docker container? can you explain it? – Tailor – 2018-11-12T13:46:33.310

2What do you mean by "run a C-File"? Do you mean to run a C program compiled to run on different hardware which you emulate? Or compile a C program using a compiler for different hardware running in an emulator? Or are you looking for a C interpreter? Or what? – AFH – 2018-11-12T13:47:08.273

Agree with @AFH – this question does not make much sense. Could you please explain the context of your problem? – slhck – 2018-11-12T13:52:02.627

I had the idea of emulating a arm cpu to run different programs. To begin with, I wanted emulate the cpu and run for example a simple hello world written in C. Sry for my bad explanation – Tailor – 2018-11-12T13:55:07.863

1Given this, the answer may not make a lot of sense. Do you want to test how or whether a program you've developed can be compiled for and run on an ARM platform? (Please respond to people with @username, otherwise they will not get a notification.) – slhck – 2018-11-12T14:07:31.897

Yes, thats what I am trying to do – Tailor – 2018-11-12T14:10:27.970

1The simple answer is that you would have to compile (and link) a C program before you could execute it with an ARM emulator (or any CPU for that matter). You could cross-compile your C program on the host (the most sensible choice) with a cross toolchain, or natively compile it with the ARM emulator if it has an OS and toolchain installed. – sawdust – 2018-11-12T21:18:53.993

Answers

0

The answer is no. A CPU only understands CPU instructions, aka assembler language. When you have a C-File, and you want to run it, you need an interpreter to make the CPU understand the script. This means the script is compiled into a binary executable which is a list of assembly instructions that the CPU will understand.

But if your CPU emulator has some kind of interface and operating system, then you can use that operating system to run a compiler and in that case you can run a C-File through the compiler. But the operating system is the GUI you want to avoid, so... that leads me back to the first paragraph.

LPChip

Posted 2018-11-12T13:19:01.247

Reputation: 42 190

@LPChip See the OP's comment on the question. I think they may be trying something different – I'm also not sure what specifically you are talking about with “your CPU emulator”. Of course a C file can never be run directly, but I think there's a more practical use case here that the OP wants to target. – slhck – 2018-11-12T14:10:14.797

1@slhck , IMHO, LPChip had clarified the technical definition of 'barebone' CPU vs 'stripdown' PC to the OP. Which made the OP to asked this question in the 1st place. I would answer a straight 'no' as well.. but the explanation helps the OP to understand that he/she'll need to have the program in assemble language/hex for his/her purpose, instead of the 'normal' c code compiler/build/run setup. ( : – p._phidot_ – 2018-11-12T16:28:29.510

" CPU instructions, aka assembler language" -- Wrong, assembly language is not machine code. "a binary executable which is a list of assembly instructions" -- Wrong, assembly instructions are not machine code. Assembly language uses mnemonics, whereas machine code has numeric opcodes. Assembly language has features such as labels, that get properly evaluated as code/data is added/removed. Whereas machine code only has numeric addresses, that would have to be manually recalculated as the code is changed. – sawdust – 2018-11-12T20:57:49.083

1*"When you have a C-File, and you want to run it, you need an interpreter to make the CPU understand the script"* -- Your (careless?) use of words that also have specific technical meanings could lead to incorrect understanding. "But the operating system is the GUI..." -- Not every OS has or needs a GUI. – sawdust – 2018-11-12T21:02:50.053

1

Your question was whether you can test a given C file on a CPU architecture different from the machine you're working on. For example, you may have a Windows machine with an Intel processor, but want to try the program on an ARM platform. This can of course be done.

The thing to note is that C code never runs directly on a CPU; it first has to be translated (compiled) into machine code, as different platforms require different machine code to do the same thing. This is what a compiler does.

You can tell a compiler to compile your code into a different target platform. This is called cross-compiling. This blog post, for example, describes how to cross-compile a Hello World program to ARM using the arm-linux-gnueabi-gcc compiler (which you can get under Ubuntu from apt install gcc-arm-linux-gnueabi).

I should add that these sort of things are somewhat easier under Linux hosts than Windows, but maybe that's a personal bias.

Once you have cross-compiled your program, you may want to test it on the target platform. For this, you will need a CPU emulator, of course, but that emulator alone will not be enough, as you need an operating system on top of it,* which can load the respective compiled C program and execute it as a process. The ARM Lab VM does that for ARM. It's a virtual machine that emulates an ARM platform on any host platform.

* Unless that program itself is “bare-metal”, which means that it runs without an intermediate operating system.

slhck

Posted 2018-11-12T13:19:01.247

Reputation: 182 472

1"... as you need an operating system..." -- Unless you have a self-contained, aka standalone or bare-metal, program. – sawdust – 2018-11-12T21:10:11.353