4
\$\begingroup\$

This is a program I wrote to convert binary to ASCII. I am still learning ARM assembly. How can I improve my program? Are there any bad programming habits in my code?

I've tested it on Raspberry Pi and Android.

@
@ ARM assembly programm which converts binary to ASCII
@ written by Kyle Kersey, (C) 2015
@
.global main
main:
 adr r2, content @ load address of binary string
 mov r3, #0 @ bit counter
 mov r4, #0 @ byte value
 b loop
loop:
 ldrb r0, [r2], #1 @ load next byte from memory
 cmp r0, #0 @ check for null terminator
 beq end_loop @ quit loop
 sub r0, r0, #48 @ convert '0' or '1' to 0 or 1 to a bit
 lsl r4, r4, #1 @ byte<<1
 add r4, r4, r0 @ byte+=bit
 add r3, r3, #1 @ increment bit counter
 cmp r3, #8 @ if at 8th bit
 moveq r0, r4 @ move byte to r0 for printing 
 moveq r4, #0 @ clear byte
 moveq r3, #0 @ clear bit counter
 bleq printChar @ print byte, branch with link
 b loop @ continue loop, branch without link
end_loop:
 mov r0, #10 @ ascii new line
 bl putchar @ print new line
 mov r7, #1 @ return 0
 swi 0 @ call software interupt 0
printChar:
 push {r2, lr} @ store content and return address
 bl putchar @ print ascii character stored in r0
 pop {r2,lr} @ restore the content and return address
 mov pc, lr @ set the return address
charFormat:
 .asciz "%c"
 .align 4
content:
 .ascii "0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100000円"
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Feb 22, 2015 at 2:25
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Well for a starter a bit better description. I have no idea what you mean by Binary to ASCII. One is a number representation system the other is a character encoding format I have no idea what you mean when you convert one to the other. You could probably solve the situation with an example. \$\endgroup\$ Commented Feb 22, 2015 at 2:51
  • \$\begingroup\$ @LokiAstari it converts a textual string of binary bits and converts it to understandable english text. \$\endgroup\$ Commented Feb 22, 2015 at 3:00
  • 2
    \$\begingroup\$ Don't tell me in a comment. Add it to the question. But that still does not make that much sense. If its a text string (then it is already encoded in ASCII (or some other encoding)) so should be readable already So what are you doing to it to make it understandable English. Are yuo converting ASCII encoded binary value into an ASCII encoded decimal number? \$\endgroup\$ Commented Feb 22, 2015 at 3:06

1 Answer 1

5
\$\begingroup\$

XOR instead of MOV

In your main subroutine, you move the value 0 into two registers.

A more efficient way to do that is to XOR the register with itself, like so:

xor r3, r3

Unnecessary branching

In your main subroutine, you branch over to the loop subroutine.

Since the loop subroutine is right beneath the main subroutine, you do not need a branch there since execution will just "fall through" to the loop subroutine.

Labels

I'm not sure how this works in ARM, but you seem to be using only global labels in your program, when there should be some local labels.

Local labels exist inside global labels, and they act kind of like a subroutine for a subroutine.

A change I might make is make the end_loop label a local label.

For more information on local labels, see here

For information on the syntax of a local label, see here

Disclaimer: I am not too familiar with ARM assembly, although I am familiar in other assemblies; some things I tell you might not work

answered Feb 22, 2015 at 3:38
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.