Saturday 9 July 2022

RISC-V Assembly input

 Previously I have managed to write an assembly program to display a range of memory locations.  A small next step is to work out how accept input to the program.  As I am using GLIBC these functions should be straightforward, in fact the simplist approach is to write a C program, see how the compiler has converted it to assembly then adapt it for my own program.

Character input

Step 1 is to read in a character from the terminal. Linux uses "blocking" input by default which means that you need to hit Enter after the character(s) before they are processed.




Command line arguments

Step 2 is to read input from command line arguments into the program.  Linux puts the arguments onto the stack for me - probably as part of the crt0 initialisation so I just need to decode the structure from the stack.


String Input

Step 3 is to read in a string from the terminal.  I can either use the stack to store the string or data storage within the program, I checked that both worked.

Non-blocking input

Finally I tried non-blocking input.  This required a bit more investigation within the C environment.  I found a beautifully clear tutorial written by Paige Ruiten as part of his snaptoken project to implement the kilo text editor.  I tinkered with his example to minimise it so that I could look at the assembly.

When someone shows you what to do it isn't too difficult but I wouldn't really like to work it all out for myself.  In fact I didn't implement this in assembly, but it is all ready for when I want to use it.

Libraries

Whilst working with these C and assembly programs I did think about how I use libraries.  I am using GLIBC in my assembly programs because I dont want to write lots of I/O routines but generally speaking this means I don't need to write assembly myself since my programs could be written in C.

However, using RISC-V, one of the benefits is being able to use native RISC-V assembly so I will continue to try a little bit.

If I write anything significant I should put it in a library.  I followed a very good opensource.com tutorial to familiarise myself with creating and using a Linux library.

No comments:

Post a Comment