1

With a background in programming, I find I am still having a tough time with assembly language concepts. All tutorials online that I have found so far are somewhat helpful but I find myself jumping from tutorial to tutorial to piece things together. Anyway I have several conceptual questions regarding the following ARM assembly program and appreciate any help and pointers.

@converts a lowercase letter to an uppercase letter 
.global _start

_start:
  MOV R7, #3
  MOV R0, #0
  MOV R2, #1
  LDR R1, =character
  SWI 0

_uppercase:
  LDR R1, =character
  LDR R0, [R1]
  BIC R0, R0, #32
  STR R0, [R1]

_write:
  MOV R7, #4
  MOV R0, #1
  MOV R2, #1
  SWI 0

end:
  MOV R7, #1
  SWI 0

.data
character:
  .ascii " "
  1. I understand what system calls are, such as read from keyboard or output to screen, but what determines what register is in charge of what system calls and how do I know what values need to be passed to those registers?
  2. In the example, register R0 is used for a syscall in line 6 to take input from the keyboard and then used again in line 13 to load register R1 address into itself. What happens to the original value/syscall from line 6? Is each line executed line by line during runtime? Where is the information stored that I am going to take input from the keyboard (line 6)?
  3. Similar to question 2, I am storing the address of a word to register R0 in line 15, but making a system call in line 19 to output to memory using the same register. What happened to the memory address that was stored in register R0 from line 15?
  4. Why does each label section have to have an SWI 0? Why does the _uppercase section not have an SWI 0?
  5. Why does the termination of the program require a syscall (line 24) before SWI 0?

Thanks in advance for answers and I hope this post can help others understand some ARM concepts.

  • 1. what registers have to be set how is written in the API, unfortunately you don't state what OS you're using – Tommylee2k Jun 01 '18 at 09:12
  • 2. after reading the character from STDIN (stdin = 0, that's why R0 is set to 0 before calling the read(), and obviously R2 is the number of bytes to read), you don't need it anymore. the call is done, R0 can be used again. yes - each instruction can be viewed as executed line-by-line. – Tommylee2k Jun 01 '18 at 09:14
  • 4 "make upper case" is not a part of the OS, it's simply done by OR-ing the characters bit 5. "SWI 0" is - as you said - used to call OS functions – Tommylee2k Jun 01 '18 at 09:18
  • 1
    5. "SWI 0" is just a generic "call the OS" dispatcher. The "mov R7, #1" in line 24 just tells the "SWI 0" which function (obviously nr.1 is "return to OS") should be executed. like the "mov R7, #3" in line 5 told the first "SWI 0", that a character should be read ( read is function nr. 3) – Tommylee2k Jun 01 '18 at 09:20
  • @Tommylee2k Thank you very much. This clarifies a lot of things. I am using Raspbian btw. – southwickIO Jun 01 '18 at 09:28
  • this could also help: https://stackoverflow.com/questions/12946958/what-is-the-interface-for-arm-system-calls-and-where-is-it-defined-in-the-linux , yours seems to be "EABI" ( call via `swi 0` , function ID in R7, params in R0-R6, return value in R0) – Tommylee2k Jun 01 '18 at 09:36
  • If you know any C or other low-level language, looking at (optimized) compiler output for simple and very-simple functions is a great way to find out how things are done. see [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116). – Peter Cordes Jun 01 '18 at 10:11
  • system calls are no different than library calls, think about fopen, how do you know what parameters and types for those parameters to fopen? You look it up right? What about fwrite? fread? you just look it up. Now system calls are not universal to arm or programming etc. A specific operating system and perhaps version has a specific set of system calls with specific parameters and registers used to pass them per system call. But it is still just a matter of looking it up... – old_timer Jun 01 '18 at 11:58

0 Answers0