Friday, 30 June 2017

assembly - How can I store 1byte value in 64bit register only using 64bit registers?



I need to write a pixelization assembly code ONLY using %rax, %rbx, %rcx, %rdx, %rsi, and %rdi (also %rsp and %rbp)




So I've first wrote code in C and changed any other registers into 64 bit registers, but at the point below when I change the register it gives Segmentation default



C code:



*temp = b;
*(temp + 1) = g;
*(temp + 2) = r;


Assembly code By gcc:




movq    -48(%rbp), %rax  
movl %eax, %edx
movq -16(%rbp), %rax
movb %dl, (%rax)
movq -16(%rbp), %rax
addq $1, %rax
movq -56(%rbp), %rdx
movb %dl, (%rax)
movq -16(%rbp), %rax

addq $2, %rax
movq -64(%rbp), %rdx
movb %dl, (%rax)


Changed %dl to %rdx:



movq    -16(%rbp), %rax
movq -48(%rbp), %rdx
movzbq (%rdx), %rbx

movq %rbx, (%rax)
movq -16(%rbp), %rax
addq $1, %rax
movq -56(%rbp), %rdx
movzbq (%rdx), %rbx
movq %rbx, (%rax)
movq -16(%rbp), %rax
addq $2, %rax
movq -64(%rbp), %rdx
movzbq (%rdx), %rbx

movq %rbx, (%rax)

Answer



I think you want to do something like:



 t = r & 0xff;
u = *temp & ~ 0xfful;
*temp = u | t;
t = (g & 0xff) << 8;
u = *temp & ~ 0xff00ul;

*temp = u | t;
t = (b & 0xff) << 16;
u = *temp & ~0xff00000ull;
*temp = u | t;


You should be able to write this with 64bit regs only. You should also be able to find a whole bunch of ways to make this way smaller than this.


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...