[dorkbotpdx-blabber] assembler on the arduino?

Greg Borenstein greg.borenstein at gmail.com
Sun Jan 27 22:53:03 EST 2008


Hey Paul,

Thanks! That looks like it'll be really helpful once I'm up and  
running. I guess the first thing I was looking for was some advice on  
how to run assembler on the Arduino at all.

Any tips on that? Even if they're really basic, they'll be a place to  
get started...

Thanks,

Greg

On Jan 27, 2008, at 1:49 PM, Paul Stoffregen wrote:

> It's really pretty easy, at least to do simple things.  If you start  
> manipulating complex data structures, assembly gets tedious pretty  
> quickly.
>
> I don't have any beginner advise, but here's some chunks of code you  
> might fine helpful for sending stuff out the serial port.
>
>
>
>
>       ;initialize uart, transmit only, 115200 baud
> init_uart:
>       ldi     r16, (ubrr_value >> 255)
>       sts     UBRR0H, r16
>       ldi     r16, (ubrr_value & 255)
>       sts     UBRR0L, r16
>       ldi     r16, 0
>       sts     UCSR0A, r16
>       ldi     r16, (1<<UCSZ1)|(1<<UCSZ0)              ;1 stop bit
>       ;ldi    r16, (1<<UCSZ1)|(1<<UCSZ0)|(1<<USBS)    ;2 stop bits
>       sts     UCSR0C, r16
>       ldi     r16, (1<<TXEN)
>       sts     UCSR0B, r16
>       ret
>
> cout:
>       push    r16
> cout_wait:
>       lds     r16, UCSR0A
>       sbrs    r16, UDRE
>       rjmp    cout_wait
>       pop     r16
>       sts     UDR0, r16
>       ret
>
> newline:
>       push    r16
>       ldi     r16, 13
>       rcall   cout
>       ldi     r16, 10
>       rcall   cout
>       pop     r16
>       ret
>
> phex:
>       push    r16
>       swap    r16
>       andi    r16, 15
>       subi    r16, 10
>       brcs    phex_2
>       subi    r16, 249
> phex_2: subi    r16, 198
>       rcall   cout
>       pop     r16
> p1hex:  push    r16
>       andi    r16, 15
>       subi    r16, 10
>       brcs    phex_3
>       subi    r16, 249
> phex_3: subi    r16, 198
>       rcall   cout
>       pop     r16
>       ret
>
>
> .macro pstr
>       rcall   pstr_string_addr
>       .dw     (@0 * 2)
> .endmacro
>
>
>       ;this complex pstr allows the caller to place the string
>       ;address in memory immediately following the call instruction,
>       ;an no register values are modified by this function.  No need
>       ;to use LDI instructions to pass the string address in the
>       ;registers, and push/pop instructions before/after the LDI
>       ;and call.  This function preserves all register values, gets
>       ;the string address from the next code word after the call,
>       ;and re-writes the return address on the stack so it returns
>       ;properly to the caller's code after the string is printed
> pstr_string_addr:
>       push    Zl
>       push    Zh
>       push    r24
>       push    r25
>       in      Zl, spl
>       in      Zh, sph
>       ldd     r25, Z+5        ;grab return address from stack
>       ldd     r24, Z+6
>       adiw    r24, 1
>       std     Z+5, r25        ;inc return address on stack
>       std     Z+6, r24
>       sbiw    r24, 1
>       lsl     r24             ;translate code space addr
>       rol     r25
>       mov     Zl, r24
>       mov     Zh, r25
>       lpm     r24, Z+         ;fetch address of string
>       lpm     r25, Z+
>       mov     Zl, r24
>       mov     Zh, r25
>       pop     r25
>       pop     r24
>       push    r16
> pstr_s_loop:
>       lpm     r16, Z+         ;fetch string bytes
>       tst     r16
>       breq    pstr_s_end      ;finish if null terminator
>       rcall   cout
>       rjmp    pstr_s_loop
> pstr_s_end:
>       pop     r16
>       pop     Zh
>       pop     Zl
>       ret
>
>
> ;to use this, write something like "pstr my_debug_msg" in your code
> ;and at the end of all you code, write 'my_debug_msg: .db "My debug  
> message",0'
>
>
>
>
>
>
>
> Greg Borenstein wrote:
>> Hey y'all,
>>
>> I was wondering if anyone had any advice for someone who wants to  
>> use the Arduino as a platform for learning Assembler. Are there any  
>> good guides out there for getting started writing and running  
>> assembler on the Arduino? Any truly beginner level texts for  
>> writing Assembler in general?
>>
>> I'm an upper-intermediate level Arduino programmer with no  
>> assembler experience. Starting tomorrow, I'll be auditing a class  
>> at Reed on language design and threading for the micro-controller  
>> environment that's going to be using the Arduino as a test subject  
>> and I wanted to try to get some of the basics under my belt.
>>
>> Thanks,
>>
>> Greg
>> ---
>> http://grabb.it/users/greg
>> http://urbanhonking.com/ideasfordozens
>> http://atduskmusic.com
>> _______________________________________________
>> dorkbotpdx-blabber mailing list
>> dorkbotpdx-blabber at dorkbot.org
>> http://music.columbia.edu/mailman/listinfo/dorkbotpdx-blabber
>>
>>
>
> _______________________________________________
> dorkbotpdx-blabber mailing list
> dorkbotpdx-blabber at dorkbot.org
> http://music.columbia.edu/mailman/listinfo/dorkbotpdx-blabber



More information about the dorkbotpdx-blabber mailing list