Ultimate Smart Move: [A+B*C], LDR =I, & More! - Raspberry Pi Forums


download smart-move + examples

new "smart-move" immediate pc relative load nearby literal table - ldr r, [pc, p-$-8] - or optional movw/movt fasmarm. =i prefix "move 32bit immediate" (compatible gas/gcc). =& pc relative load. scroll down easy-move+dp upgrade (add, sub, cmp, etc, supports i/[m]/[a+b*c]).

code: select all

; literal.inc  literals equ  ; literal table literals.i=-1 ; offset  ; define literal 32bit number. attach line/data ; definition: literals=itself+dw x  macro .literal x {  literals equ literals, dw x  literals.i=literals.i+4 }  ; store literal table or initialize  macro .literals {  local ..literals  ?literals \          ; address   equ ..literals  match j, literals \{ ; expand a,b,c   irp i, j \\{        ; expand each line                     ; dw 1, dw 2, dw 3   \\}   restore ?literals  \}  match , literals \{  ; initialize   literals equ \      ; first lines...   align 4,\   ?literals:,\        ; begin   literals.i=0        ; offset  \} }  ; move wide/top 32bit  use.movwt? equ 0   ; 1 if cpu>=arm.v6t2  macro movwt r, {  i=i , 0ffffh  movw r,         ; low 16bit  i=(i shr 16) \   , 0ffffh  if i<>0           ; high 16bit?   movt r,  end if }  ; generic move 32bit immediate (worst ; case scenerio)  macro movi r, {  local n  n=i , 0ffh  mov r, n             ; mov r, b&ffh  n=(i , 0ff00h)  if n<>0              ; if 16+bit...   orr r, n            ; orr r, r, b&ff00h  end if  n=(i , 0ff0000h)  if n<>0   orr r, n            ; orr r, r, b&ff0000h  end if  n=(i , 0ff000000h)  if n<>0   orr r, n            ; orr r, r, i&ff000000h  end if }
upgrade ldr instruction match sequences. =i "move 32bit immediate"; mov/mvn, constant rotation or movw/movt (if supported, cpu>=arm.v6t2). &i pc relative load literal table:

code: select all

; ldr r, [pc, p-$-8+4]    ; ldr r, &77777777h ; .literal 77777777h  use.mov? fix \  (i>=0 & i<=255)\         ; use mov?  | (i=-1 | i=$ffffffff)\  ; use mvn?  | (i , $fffff00f)=0\   ; use mov+ror?  | (i , $ffff00ff)=0\   ; ff00h  | (i , $fff00fff)=0\   ; ff000h  | (i , $ff00ffff)=0\   ; ff0000h  | (i , $f00fffff)=0\   ; ff00000h  | (i , $00ffffff)=0    ; ff000000h  macro ldr [p] {  common   local i, x   define ?s 0   match r=, &n, p \{         ; ldr r, &i    x=(?literals+\            ; pc relative     literals.i)-$-8+4        ; address    ldr r, [pc, x]            ; load    .literal n                ; store    define ?s 1               ; matched   \}   match =0 \                 ; else    r=,==n, ?s p \{           ; ldr r, =i    i=(n)    if use.mov?               ; use mov?     mov r,    else if use.movwt?        ; use movw/movt?     movwt r,               ; cpu>=arm.v6t2    else                      ; worst case     movi r,                ; scenerio: mov+orr    end if    define ?s 1               ; matched   \}   if ?s eq 0                 ; else, use    ldr p                     ; original ldr   end if                     ; instruction }
test new ldr instruction:

code: select all

include 'literal.inc'  .literals          ; initialize: p=$, i=0  ldr r0, [r1]       ; original ldr ldr r0, =-1        ; mvn ldr r0, =80h       ; mov... ldr r0, =7f0h ldr r0, =7f00h ldr r0, =7f000h ldr r0, =7f0000h ldr r0, =7f00000h ldr r0, =7f000000h ldr r0, =80000000h ldr r0, =1234h     ; movw/movt or mov+orr ldr r0, &11111111h ; ldr r, [pc, p-$-8]... ldr r0, &22222222h ldr r0, &33333333h ldr r0, &44444444h bkpt 7777h  .literals          ; store constants
disassembly:

code: select all

00000000 e5910000 ldr r0, [r1] 00000004 e3e00000 mvn r0, 0 00000008 e3a00080 mov r0, 80h 0000000c e3a00e7f mov r0, 7f0h 00000010 e3a00c7f mov r0, 7f00h 00000014 e3a00a7f mov r0, 7f000h 00000018 e3a0087f mov r0, 7f0000h 0000001c e3a0067f mov r0, 7f00000h 00000020 e3a0047f mov r0, 7f000000h 00000024 e3a00102 mov r0, 80000000h 00000028 e3a00034 mov r0, 34h 0000002c e3800c12 orr r0, r0, 1200h 00000030 e59f000f ldr r0, &11111111h 00000034 e59f000f ldr r0, &22222222h 00000038 e59f000f ldr r0, &33333333h 0000003c e59f000f ldr r0, &44444444h 00000040 e1277777 bkpt 7777h 00000044 dw ...
upgrade function/proc insert literal table. note: custom inheritance in fasm's language supersedes c++/java's concept of oop.

code: select all

macro function [p] {  common   function p ; function=itself/previous   .literals  ; +this/new }  macro endf {  .literals   ; endf=this/new  endf        ; +itself/previous }
new! easy-move+dp upgrade (add, sub, cmp, etc): supports immediate, [memory] , [s*i+b] syntax. example:

code: select all

include 'literal.inc'  mov r1, r2 mov r1, 80000000h mov r1, [r2] mov [r1], r2 mov r1, [r2+r3] mov [r1+r2*4], r3 mov r1, [r2+123abch]  add r1, r2 add r1, 80000000h add r1, [r2] add [r1], r2 add r1, [r2+r3] add [r1+r2*4], r3 add r1, [r2+123abch]  sub r1, r2 orr r1, 80000000h , r1, [r2] bic [r1], r2 sub r1, [r2+r3] cmp [r1+r2*4], r3 mvn r1, [r2+123abch]
disassembly:

code: select all

00000000 e1a01002 mov r1, r2 00000004 e3a01102 mov r1, 80000000h 00000008 e5921000 ldr r1, [r2] 0000000c e5812000 str r2, [r1] 00000010 e7921003 ldr r1, [r2, r3] 00000014 e7813102 str r3, [r1, r2, lsl 2] 00000018 e3a0c0bc mov r12, 0bch 0000001c e38ccc3a orr r12, r12, 3a00h 00000020 e38cc812 orr r12, r12, 120000h 00000024 e792100c ldr r1, [r2, r12] 00000028 e0911002 adds r1, r1, r2 0000002c e2911102 adds r1, r1, 80000000h 00000030 e592b000 ldr r11, [r2] 00000034 e091100b adds r1, r1, r11 00000038 e591a000 ldr r10, [r1] 0000003c e09aa002 adds r10, r10, r2 00000040 e581a000 str r10, [r1] 00000044 e792b003 ldr r11, [r2, r3] 00000048 e091100b adds r1, r1, r11 0000004c e791a102 ldr r10, [r1, r2, lsl 2] 00000050 e09aa003 adds r10, r10, r3 00000054 e781a102 str r10, [r1, r2, lsl 2] 00000058 e3a0c0bc mov r12, 0bch 0000005c e38ccc3a orr r12, r12, 3a00h 00000060 e38cc812 orr r12, r12, 120000h 00000064 e792b00c ldr r11, [r2, r12] 00000068 e091100b adds r1, r1, r11 0000006c e0511002 subs r1, r1, r2 00000070 e3811102 orr r1, r1, 80000000h 00000074 e592b000 ldr r11, [r2] 00000078 e011100b ands r1, r1, r11 0000007c e591a000 ldr r10, [r1] 00000080 e1daa002 bics r10, r10, r2 00000084 e581a000 str r10, [r1] 00000088 e792b003 ldr r11, [r2, r3] 0000008c e051100b subs r1, r1, r11 00000090 e791a102 ldr r10, [r1, r2, lsl 2] 00000094 e15a0003 cmp r10, r3 00000098 e781a102 str r10, [r1, r2, lsl 2] 0000009c e3a0c0bc mov r12, 0bch 000000a0 e38ccc3a orr r12, r12, 3a00h 000000a4 e38cc812 orr r12, r12, 120000h 000000a8 e792b00c ldr r11, [r2, r12] 000000ac e1e0100b mvn r1, r11
low-level get/set helper macros. usage:

code: select all

@get r1, r2           ; r=r/[m]/i @get r1, 10000h @get r1, [r2] @get r1, [20000h] @get r1, [r2+30000h] @get r1, [30000h+r2] @get r1, [r2+r3*4]  @set [r1], r2         ; [m]=r @set [20000h], r1 @set [r1+30000h], r2 @set [40000h+r1], r2 @set [r1+r2*4], r3

code: select all

; registers calculations  @r fix r12  ; spare @sr fix r11 ; source @dr fix r10 ; destiny  is.i? fix eqtype 0  is.r? fix in <r0,r1,r2,r3,r4,r5,r6,r7,\  r8,r9,r10,r11,r12,r13,r14,r15>  macro @get r, x {  define ?s 0  match \   [a], x \{   match \    [b+i], x \\{    match \     i*s, \\\{              ; [a+b*c]     if s eq 4      ldr r, [b, i, lsl 2]     else      'error'     end if     define ?s 1    \\\}    if ?s eq 0                ; [a+b]     if b is.r? \             ; [r+r]      & is.r?      ldr r, [b, i]     else if b is.r? \        ; [r+i]      & is.i?      ldr @r, =i      ldr r, [b, @r]     else if is.r? \        ; [i+r]      & b is.i?      ldr @r, =b      ldr r, [@r, i]     else      'error'     end if    end if    define ?s 1   \\}   if ?s eq 0    if is.r?                ; [r]     ldr r, [a]    else                      ; [m]     ldr @r, =a     ldr r, [@r]    end if   end if   define ?s 1  \}  if ?s eq 0   if x is.r?                 ; r    mov r, x   else                       ;    ldr r, =x   end if  end if }  macro @set x, r {  define ?s 0  match \   [a], x \{   match \    [b+i], x \\{    match \     i*s, \\\{              ; [a+b*c]     if s eq 4      str r, [b, i, lsl 2]     else      'error'     end if     define ?s 1    \\\}    if ?s eq 0                ; [a+b]     if b is.r? \             ; [r+r]      & is.r?      str r, [b, i]     else if b is.r? \        ; [r+i]      & is.i?      ldr @r, =i      str r, [b, @r]     else if is.r? \        ; [i+r]      & b is.i?      ldr @r, =b      str r, [@r, i]     else      'error'     end if    end if    define ?s 1   \\}   if ?s eq 0    if is.r?                ; [r]     str r, [a]    else                      ; [m]     match [i j], x      \\{ 'error' \\}     ldr @r, =a     str r, [@r]    end if   end if  \}  if ?s eq 0   'error'  end if }
upgrade mov support r/[r]/[m]/i:

code: select all

macro mov [p] {  common   define ?s 0   match r=,[m], p \{    if m is.r?     ldr r, [m]    else     @get r, [m]    end if    define ?s 1   \}   match [m]=,r, p \{    if m is.r?     str r, [m]    else     @set [m], r    end if    define ?s 1   \}   if ?s eq 0    mov p   end if }
upgrade data processing instructions - add, sub, cmp, orr, and, tst, bic, etc - support r/[r]/[m]/i.:

code: select all

macro @dp name, [p] {  common   define ?s 0   match r=,[m], p \{    mov @sr, [m]    name r, @sr    define ?s 1   \}   match [m]=,r, p \{    mov @dr, [m]    name @dr, r    mov [m], @dr    define ?s 1   \}   if ?s eq 0    name p   end if }  macro , [p] { common @dp ands, p } macro eor [p] { common @dp eors, p } macro sub [p] { common @dp subs, p } macro rsb [p] { common @dp rsbs, p } macro add [p] { common @dp adds, p } macro adc [p] { common @dp adcs, p } macro sbc [p] { common @dp sbcs, p } macro rsc [p] { common @dp rscs, p } macro tst [p] { common @dp tst, p } macro teq [p] { common @dp teq, p } macro cmp [p] { common @dp cmp, p } macro cmn [p] { common @dp cmn, p } macro bic [p] { common @dp bics, p } macro mvn [p] { common @dp mvn, p }
download smart-move + examples
my web site: programming, examples, tutorials

you've misunderstood arm modified constants. of following (and, of course, rotations of same 4, 8, 12, 16, 20, 24, or 28) should one-instruction loads using mov or mvn.

code: select all

ldr r0, =0x000000ff ldr r0, =0x000003fc ldr r0, =0xffffff00 ldr r0, =0xfffffc03 


raspberrypi



Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62