We recently started to learn about assembly code and we have homework on the topic. We have been studying the ARM and have to code in Raspberry Pi. One of my homework questions read like this:
Write assembly functions that implement the following C functions:
int32_t shiftS32(int32_t x, int32_p) // return x*2^p for p = -31..31
This is my answer to the question:
.global shiftS32
.text
shiftS32:
PUSH {R0}
CMP R0, #0
BMI ENDA
PUSH {R1}
CMP R1, #0
BMI END1
POP {R1}
MOV R0, R0, LSL R1
BX LR
END1:
POP {R1}
SUB R1, #0
NEG R1, R1
MOV R0, R0, LSR R1
BX LR
ENDA:
PUSH {R1}
CMP R1, #0
BMI END
POP {R1}
MOV R0, R0, LSL R1
BX LR
END:
POP {R1}
SUB R1, #0
NEG R1, R1
MOV R0, R0, ASR R1
BX LR
This code works but I think I am overdoing this. Is there a way to do this same thing but in fewer lines?