0

I have just got into ARM programming. Learned a few basics but am having issues with running code. I use VSCode on Linux Subsystem as my IDE.

I have nothing installed on my computer and i would like to run ARM code. I have read online something about "qemu" and "kernel" and stuff, but am not sure what they mean. It would be great if someone provides a detailed walkthrough for such a problem. I do not have a raspberry pi.

For example, how do i run the following division.s file on VSCode?

 .global _start
_start:
        MOV R1, #X
        MOV R2, #Y
        MOV R3, #Z
        CMP R1, R2 @ is x>y ?
        BGT _tryx
        CMP R2, R3 @ is y>z ?
        BGT _isy
        MOV R4, R3
        B _exit
_isy:
        MOV R4, R2
        B _exit
_tryx:
        CMP R1, R3 @ is x>z ?
        BGT _isx
        MOV R4, R3
        B _exit
_isx:
        MOV R4, R1
_exit:
        MOV R0, R4
        MOV R7, #1
        SWI 0
.data
.equ X, 3
.equ Y, 5
.equ Z, 4

Are there any extensions i need to install? Is there anything i need to download? I have used gcc to compile C code. Can it be used here too?

Thx in advance! :D

Frant
  • 5,382
  • 1
  • 16
  • 22

2 Answers2

2

Your question is rather a broad one. This being said, a slightly modified version of your program can be executed in WSL using the following procedure:

sudo apt-get install qemu-user
sudo mkdir -p /opt/arm/10
wget 'https://developer.arm.com/-/media/Files/downloads/gnu-a/10.2-2020.11/binrel/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz?revision=d0b90559-3960-4e4b-9297-7ddbc3e52783&la=en&hash=985078B758BC782BC338DB947347107FBCF8EF6B' -O gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz
sudo tar Jxf  gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz  -C /opt/arm/10

/tmp/division.s:

@ count how often we can take Y from X
  
        .global main
main:
        MOV R1, #X
        MOV R2, #Y
        MOV R3, #0 @ Q
_loop:
        CMP R1, R2
        BLT _exit
        SUB R1, R2
        ADD R3, #1
        B _loop
_exit:
        MOV R0, R3
        MOV R7, #1
        SWI 0
.data
.equ X, 23
.equ Y, 4

Compiling:

/opt/arm/10/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc -static -o /tmp/division /tmp/division.s

Executing - WSL:

qemu-arm /tmp/division
echo $?
5

Which is the expected result, since 23 div 4 is 5.

Executing - Windows 10:

C:\>c:\Windows\System32\bash -c "qemu-arm /tmp/division; echo $?"
5

C:\>

Or:

C:\>c:\Windows\System32\bash -c "qemu-arm /tmp/division"
C:\>echo %ERRORLEVEL%
5

Note that division.s may have been compiled in Windows 10 as well by downloading/installing gcc-arm-10.2-2020.11-mingw-w64-i686-arm-none-linux-gnueabihf.tar.xz instead of gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf.tar.xz - your choice.

I let it to you than to go into the details of using the information above for running your program from vscode, you question being a bit too broad IMHO.

Update: division.s was compiled statically on purpose for avoiding having to specify the locations for any non-needed dynamic libraries.

Compiling it without using the -static option, and executing it would result in the following error message to be displayed:

qemu-arm division
/lib/ld-linux-armhf.so.3: No such file or directory

It can be avoided by telling qemu-arm where to look for required dynamic libraries, /lib/ld-linux-armhf.so.3 in this case:

qemu-arm -L /opt/arm/10/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/arm-none-linux-gnueabihf/libc division
echo $?
5
Frant
  • 5,382
  • 1
  • 16
  • 22
  • Tysm, That works! ..but only for the code i specified above in my question. When i change the code to some other program, it returns an error. Note that i have only changed the contents of `division.s` so that i don't have to change the commands u gave me above. Is there something else i need to take care of when running the code? – Abdullah Meda Feb 11 '21 at 17:24
  • I actually ran it on WSL, ..just for ur information – Abdullah Meda Feb 11 '21 at 17:24
  • 2
    'change the code to some other program' is not specific enough, I cannot answer if you don't provide the exact commands you are using, so that I may execute them. This being said, you may have noticed that division.s was statically compiled. If you attempted to execute a dynamic ARM executable, there are some extra steps to be performed. I may help further if you provide the exact commands you are using. – Frant Feb 11 '21 at 17:28
  • ok, i am going to edit my main qs to have the new code, btw the commands i used earlier were the exact same coz i just changed the contents of `division.s` in vscode. – Abdullah Meda Feb 11 '21 at 17:39
  • You may want to do this in a brand new question for clarity purpose. – Frant Feb 11 '21 at 17:42
  • alright sounds great, is there anything specific i should mention in the new question since u said my main qs was quite broad? – Abdullah Meda Feb 11 '21 at 17:45
  • I would say that the integration with vscode should stay on the side for the time being. Just provide the two versions of the program, the commands you used, any error message you are getting, and say that the second one is not working. – Frant Feb 11 '21 at 17:55
  • Any idea why my answer is not accepted anymore? I answered that one though, IMHO. – Frant Feb 11 '21 at 17:57
  • i unaccepted it later coz when i make my new question, it wouldnt show my new qs as duplicate, thats the only reason why. In my opinion, u did answer my qs, i will accept it again after i make the new question. sry bout that – Abdullah Meda Feb 12 '21 at 03:05
  • I have accepted ur answer, and here is a link to the new qs if u r interested :) - https://stackoverflow.com/questions/66169781/is-there-a-command-to-run-any-arm-assembly-file-s-after-having-installed-qemu – Abdullah Meda Feb 12 '21 at 10:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228630/discussion-between-frant-and-craig-hemsforth). – Frant Feb 12 '21 at 12:46
0

That looks like a Linux program. And I guess you have an 64-bit x86 computer.

You want QEMU, specifically the executable qemu-arm which emulates an ARM processor running Linux on another Linux compatible system. In Debian derived Linux distributions it is in the package qemu-arm.

And on ebian derived Linux distributions for a compiler you can install the gcc-arm-linux-gnueabihf package for compiling for ARMv7 and ARMv8 with hardware floating point (Debian port name armhf), or gcc-arm-linux-gnueabi for some older ARMs with no hardware floating point Debian port name armel).

If you wish to install libraries, you need add the architecture to dpkg's list, for example:

dpkg --add-architecture armhf
Timothy Baldwin
  • 3,551
  • 1
  • 14
  • 23
  • I did `sudo apt-get install qemu` on my ububtu terminal and then did `as program.s -o program.o` but am still getting a bunch of errors. Am i doing something wrong there? – Abdullah Meda Feb 11 '21 at 16:52
  • `qemu` is a x86 PC emulator, you need the `qemu-arm` package. The `as` command assembles for the processor type it runs on, you need the `arm-linux-gnueabihf-as` or `arm-linux-gnueabi-as`commands. – Timothy Baldwin Feb 11 '21 at 17:01