antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

ASM code not working

Sun Jun 23, 2013 8:23 am

I was having a play with using macros and wrote this little demo program.

Code: Select all

@ macros.s
@ experimenting with a couple of macros

.section    .data
nl:
    .byte 0xA   @ ascii codes
spc:
    .byte 0x20
char:             @ initially holds 65 / 'A'.
    .byte 0x41

.macro print_ch a         @ NB this macro clobbers registers r0, r1, r2 and r7.
mov r0, $1                   @ A function would be wiser. This is just a demo.
ldr r1, =\a
mov r2, $1
mov r7, $4
svc $0
.endm

.section  .text
.globl  _start
_start:

ldr r3, =char
mov r4, $0x41         @ this is the bit that confuses me. I wanted to load the number 65 from r3
                              @ but if I do "ldr r4, [r3]" the program just prints a capital 'A' and exits. This immediate load works,
                              @ but it's annoying when I have the number right there in "char".
print_ch nl
loop:
    print_ch char
    print_ch spc
    cmp r4, $0x5a       @ are we done?
    addlt r4, r4, $1
    strlt r4, [r3]            @ increment char variable.
    blt loop

print_ch nl
mov r7, $1
svc $0

.end

As you can see from my comments, my problem is I don't seem to know how to get the 65 from char into r4. :oops:

Also, if anyone with assembly code knowledge would mind looking at ths other post, I'd really appreciate it.
Question about number printing function.

User avatar
rpdom
Posts: 12861
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: ASM code not working

Sun Jun 23, 2013 10:27 am

I think you need ldrb r4, [r3] as you only want to load a byte in from that address, not the whole 32-bit word around it which will undoubtedly be higher than 0x51.

antiloquax
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
Contact: Website

Re: ASM code not working

Sun Jun 23, 2013 10:39 am

Thanks rpdom.
Of course, you are right.

Return to “Bare metal, Assembly language”