So i'm trying to rewrite a function from c into assembly, this was more of an exercise into writing assembly in C rather than making this more efficient.
The problem I am having is I have working code in three asm() blocks but I can't seem to combine them. I think there must be something i'm missing when combining them.
This is currently the code that works:
137 __asm__ __volatile__ (
138 "mov R0, #1\n\t"
139 "mov R1, %[bb]\n\t"
140 "and R0, R1\n\t"
141 "cmp R0, #1\n\t" // (b & 1) == 1
142 "bne aftereor\n\t"
143 "eor %[pp], %[pp], %[aa]\n\t"
144 "aftereor:\n\t"
145 "mov %[hbs], %[aa]\n\t"
146 "mov R0, #128 \n\t"
147 "and %[hbs], R0 \n\t"
148 "lsl %[aa], %[aa], #1\n\t"
149 : [pp]"+l" (p),[aa]"+l" (a),[hbs]"=l" (hi_bit_set)
150 : [bb]"l" (b)
151 :
152 );
153 __asm__ __volatile__ (
154 "cmp %[hbs], #128 \n\t"
155 "bne brancha \n\t"
156 "mov R2, #0x1b\n\t"
157 "eor %[aa], %[aa], R2\n\t"
158 "brancha:\n\t"
159 : [aa]"+l" (a)
160 : [hbs]"l" (hi_bit_set)
161 :
162 );
163 __asm__ __volatile__ (
164 "lsr %[bb], %[bb], #1"
165 : [bb]"+l" (b)
166 :
167 :
168 );
This is the C code I am attempting to rewrite in assembly:
if((b & 1) == 1) {
p ^= a;
}
hi_bit_set = (a & 0x80);
a <<= 1;
if(hi_bit_set == 0x80) {
a ^= 0x1b;
}
b >>= 1;
Both of the above pieces of code work as expected. However, my problem is when combining the three blocks of assembly into one. For example, the following code does not work as expected for some reason.
137 __asm__ __volatile__ (
138 "mov R0, #1\n\t"
139 "mov R1, %[bb]\n\t"
140 "and R0, R1\n\t"
141 "cmp R0, #1\n\t" // (b & 1) == 1
142 "bne aftereor\n\t"
143 "eor %[pp], %[pp], %[aa]\n\t"
144 "aftereor:\n\t"
145 "mov %[hbs], %[aa]\n\t"
146 "mov R0, #128 \n\t"
147 "and %[hbs], R0 \n\t"
148 "lsl %[aa], %[aa], #1\n\t"
149 "cmp %[hbs], #128 \n\t"
150 "bne brancha \n\t"
151 "mov R2, #0x1b\n\t"
152 "eor %[aa], %[aa], R2\n\t"
153 "brancha:\n\t"
154 "lsr %[bb], %[bb], #1"
155 : [pp]"+l" (p),[aa]"+l" (a),[hbs]"+l" (hi_bit_set),[bb]"+l" (b)
156 :
157 :
158 );
The only changes I made were combining the 2nd and 3rd blocks into the first, changing the variables 'hi_bit_set' and 'b' to be both read and write. To my understanding this seems fine to me. However this is not producing the correct result so i'm guessing I have missed something.
Thanks in advance for your help.