I have a simple program here which is set to produce the output,
The value of a is 2005
The value of b is 1959
The value of c is 1
The value of d is 0
The value of (a - b) * (c + d) is 46
I’m looking for the best ways to optimize my code, I’m repeating code ALOT! is it possible to print straight from other registers? e.g SWI 0 but for a integer not in R0?
B main
aval DEFW 2005
bval DEFW 1959
cval DEFW 1
dval DEFW 0
valOf DEFB 'The value of \0'
isStr DEFB ' is \0'
lstStr DEFB 'The value of (a - b) * (c + d) is \0'
ALIGN
main ADR R0, valOf ;Value a
SWI 3
MOV R0, #97
SWI 0
ADR R0, isStr
SWI 3
LDR R0, aval
SWI 4
MOV R0, #10 ;print new line
SWI 0
ADR R0, valOf ;Value b
SWI 3
MOV R0, #98
SWI 0
ADR R0, isStr
SWI 3
LDR R0, bval
SWI 4
MOV R0, #10 ;print new line
SWI 0
ADR R0, valOf ;Value c
SWI 3
MOV R0, #99
SWI 0
ADR R0, isStr
SWI 3
LDR R0, cval
SWI 4
MOV R0, #10 ;print new line
SWI 0
ADR R0, valOf ;print 'value of '
SWI 3
MOV R0, #100
SWI 0
ADR R0, isStr ;
SWI 3
LDR R0, dval
SWI 4
MOV R0, #10 ;print new line
SWI 0
ADR R0, lstStr
SWI 3
LDR R3, aval
LDR R4, bval
LDR R5, cval
LDR R6, dval
SUB R1, R3, R4
SUB R2, R5, R6
MUL R0, R1, R2
SWI 4
SWI 2 ;exit
Any thoughts much appreciated 🙂
I was about to ask if you have to write in assembly but since you are using
SWI(nowadays it is called SVC) obviously you are sitting on top of an OS, high chance Linux.Why you are not writing in C? That should be much easier for you except if this is solely for learning purposes which in that case why you would try to learn such a usage?
About optimization, when your are sitting on top of a non-real-time OS and make system calls any performance expectation will be just lost. From the point of software interrupt kernel can delay your code variably and un-guaranteed fashion.
So in this case if you are thinking about making your application fast, try to reduce number of system calls by preparing / caching print buffers in userspace where you can apply some plain and easy C idioms.