Search code examples
cassemblyvisual-studio-codecompilationarm

How do i run assembly (.s) files on VSCode in Linux subsystem


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


Solution

  • 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