I was reading a textbook which shows generated assembly code based on a C function:
C Code:
void proc(long a1, long *a1p,
int a2, int *a2p,
short a3, short *a3p,
char a4, char *a4p)
{
*a1p += a1;
*a2p += a2;
*a3p += a3;
*a4p += a4;
}
Generated assembly code:
proc:
movq 16(%rsp), %rax //Fetch a4p (64 bits) Line 1
addq %rdi, (%rsi) //*a1p += a1 (64 bits) Line 2
addl %edx, (%rcx) //*a2p += a2 (32 bits) Line 3
addw %r8w, (%r9) //*a3p += a3 (16 bits) Line 4
movl 8(%rsp), %edx //Fetch a4 ( 8 bits) Line 5
addb %dl, (%rax) //*a4p += a4 ( 8 bits) Line 6
ret return
Observe that the movl instruction of line 5 reads 4 bytes from memory; the following addb instruction only makes use of the low-order byte.
but I'm wondering why we don't use "movb" in the line 5 directly as:
...
movb 8(%rsp), %dl //Fetch a4 ( 8 bits)
addb %dl, (%rax) //*a4p += a4 ( 8 bits)
isn't this approach more concise and straightforward?