Assembly Language for Beginners

(Jeff_L) #1

1.16. LOOPS


SBjust writes a byte from lowest 8 bits of register to memory.


Vectorization


Optimizing GCC can do much more on this example:1.29.1 on page 412.


1.16.3 Condition check


It’s important to keep in mind that infor()construct, condition is checked not at the end, but at the
beginning, before execution of loop body. But often, it’s more convenient for compiler to check it at the
end, after body. Sometimes, additional check can be appended at the beginning.


For example:


#include <stdio.h>


void f(int start, int finish)
{
for (; start<finish; start++)
printf ("%d\n", start);
};


Optimizing GCC 5.4.0 x64:


f:
; check condition (1):
cmp edi, esi
jge .L9
push rbp
push rbx
mov ebp, esi
mov ebx, edi
sub rsp, 8
.L5:
mov edx, ebx
xor eax, eax
mov esi, OFFSET FLAT:.LC0 ; '%d\n"
mov edi, 1
add ebx, 1
call __printf_chk
; check condition (2):
cmp ebp, ebx
jne .L5
add rsp, 8
pop rbx
pop rbp
.L9:
rep ret


We see two checks.


Hex-Rays (at least version 2.2.0) decompiles this as:


void cdecl f(unsigned int start, unsigned int finish)
{
unsigned int v2; // ebx@2
int64 v3; // rdx@3


if ( (signed int)start < (signed int)finish )
{
v2 = start;
do
{
v3 = v2++;
_printf_chk(1LL, "%d\n", v3);
}
while ( finish != v2 );
Free download pdf