Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I am writing this on phone and from memory... so this won’t compile but I’ll comment so you can work out what is happening and fix it
- void arbitrary_add(uint32_t *a, uint32_t *b, uint32_t *dst, size_t size)
- {
- // GCC’s asm wrapper
- __asm__(
- ".intel_syntax;" // What flavour of assembly we are using
- <remainder of assembly code here>
- <I couldn’t be bothered to type in quotes on my phone so it is just below>
- ".att_syntax;"
- : // No outputs
- : "c"(size), “b”(a), “s”(b), “d”(dst)
- : "ecx", “eax”, “edx”, “ebx”, “esi”, “edi”); // Clobbered registers
- }
- ; Document our register choices
- ; ecx = counter
- ; eax = scratch
- ; edx = counter
- ; ebx = source A
- ; esi = source B
- ; edi = dst
- ; Assume that we have normalised vectors ebx, esi and edi to all be the same length (in practice you might need to add one extra value of zero to each of the vectors to allow for overflow)
- ; At this point GCC will push to the stack the original values in our clobbered registers and restore their value at the end for us
- ; GCC will then load ECX, EBX, ESI and EDI with the pointers for size, a, b, and dst
- ; Check if size > 0
- .initial_size_check
- CMP ecx, 0. ; compare to zero
- JE .epilogue. ; bail if equal
- ; Normally you can freely use “ADD”, “CMP” etc in our loop conditions and increment
- ; However, we want to preserve the carry flag so I have written a contrived loop
- ; where I only use “DEC” and “INC” because they preserve the carry flag by design
- XOR edx, edx ; edx is going to count up, whilst ecx counts down
- CLC ; clear carry flag
- .loop_body:
- ; Each loop iteration is going to perform dst[i] = a[i] + b[i] + carry flag
- MOV eax, dword [ebx + edx] ; load value from A vector
- ADC eax, dword [esi + edx] ; add with carry A and value from B vector
- MOV dword [edi + edx], eax ; store result into dst vector
- .loop_increment:
- inc edx ; increment our counter
- dec ecx ; decrement our size (this sets zero flag appropriately)
- .loop_condition:
- JNZ .loop_body ; Can’t use the natural “Jump Above or Equal” because it relies on carry flag
- .epilogue
- ; At this point dst contains the addition of A and B!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement