How does the "if" work?


I have that question about how if works in low level programming.

If you do the check in the RAM going through all the addresses that contain it.

asked by Hector Seguro 02.03.2017 в 23:36

1 answer


Good morning.

High level ifs are not the same as low level ones. An if is not compared in the ram, the processor loads the data from the RAM to the registers (EAX, EDX, etc. These are like "variables" that the processor has physically by means of transients and electronic elements) Then compare these records with the CMP and flow control instructions such as JNZ, JG, JL, JO. These instructions are symbolic names of binary values, this is so that the programmer can understand the program, but an assembly development environment usually looks like this:

Where you see a structure like:

[ memory address ] [ instruction code ] [ mnemonic code ] [; and sometimes comments ]

Then the CMP instruction compares two records and activates the corresponding flags [FLAGS] . Then a jump instruction like JE (jump if it is the same) determines where the program flow continues according to the corresponding flag by moving the EIP (Instruction pointer register) pointer. After this, the corresponding instruction blocks are executed.

You can see the example of high-level code vs a low-level complete one in this question that I answered some time ago. This is a part of the code, they both do the same thing:


mov     CX, offset texto1       ; Carga de variables en registros
mov     BX, offset texto1
mov     DX, offset texto1       
add     DX, largoTexto         

cmp     CX, DX                  ; Comparacion de registros CX y DX esto activa todas las banderas que correspondan
jge     ip_forend               ; Si CX es mayor o igual que DX sale del ciclo o sea salta a la direccion de ip_forend
inc     CX                      ; Si no se cumple la condicion de salto, la rutina sigue en la siguiente instruccion

mov     DI, CX                 
mov     AL, byte ptr[DI]       
cmp     AL, 32                 
jne     ip_if2                  ; otro ejemplo de If aqui!
cmp     CX, offset texto1      
je      ip_if2                  ; y aqui
mov     SI, BX                 
mov     DI, CX                 
dec     DI                     
mov     BX, CX                 
inc     BX                     
jmp     ip_while               ; este es un salto incondicional, siempre salta

mov     AX, DX                  
dec     AX                      
cmp     CX, AX                  
jne     ip_for                 ; otra comparacion aqui
mov     SI, BX                  
mov     DI, CX                  

cmp     SI, DI 
jge     ip_for
mov     AL, byte ptr[SI]
xchg    AL, byte ptr[DI]
xchg    AL, byte ptr[SI]
inc     SI
dec     DI
jmp     ip_while



for (int CX = 0, BX = 0, DX = largoTexto; CX < DX; CX++) {
    if (texto1[CX] == ' ' && CX != 0) {
        SI = BX;
        DI = CX;
        BX = CX;
    } else if (CX == DX - 1) {
        SI = BX;
        DI = CX;
    } else {

    while (SI < DI) {
        char AL = texto1[SI];
        texto1[SI] = texto1[DI];
        texto1[DI] = AL;
answered by 03.03.2017 / 00:00