Weakly understand the assembler. Tell me, please, an assembler code for such operations written on :

here it is desirable with LOOP

 total = 0; for(i = 0; i < 5; i=i+1) total = total + 1; 

and

 if(counter > 10) then counter = 0; else counter = counter + 1; 
  • need to fasm? - Lolidze
  • no, just a similar code as R0 = 5 -> MOV R0, # 5 - Aleitala Ameda
  • The code will be what the compiler will do. Therefore, you need to compile and disassemble. - user239133 9:01 pm
  • So which assembly language does the code need? You are talking about some kind of R0, and for example on the x86 architecture there is no such register (at least ax is not called R0). There are such registers for example on the S390 architecture, but there is no MOV instruction there. PS and yes, obviously, if total is in register R0, then mov R0, #4 is exactly what a normal modern optimizing compiler will do with your first example - Mike
  • one
    I work with ARM Cortex-m0 - Aleitala Ameda

3 answers 3

With the cycle (everything was done in fasm ):

 mov ecx,5 ;цикл loop работает с переменной ecx, задаем макс значение(минусуется пока ecx не будет равен 0) mov ebx,0 ;регистр, куда записываются данные l1: add ebx,1 ;прибавляем единицу loop l1 ;проверяется не равен ли ecx нулю, если нет, то минусуем и прыгаем на метку l1 ;выводим результат invoke wsprintfA, addr tr, addr Text, ebx invoke MessageBox,0, addr tr, addr caption,0 

With condition

 mov eax,5 ;изначальный регистр mov ebx,10 ;с чем сравнивать cmp eax,ebx ;cmp -команда сравнения jl m1 ;если eax меньше, чем ebx, то прыгаем на метку m1 ;если же больше, то присваиваем eax значение 0 mov eax,0 jmp m2 ;прыгаем на метку m2, что бы не началось выполнение метки m1 m1: add eax,1 ;добавляем единицу к регистру eax m2: ;выводим результат invoke wsprintfA, addr tr, addr Text, eax invoke MessageBox,0, addr tr, addr caption,0 
  • and how does the cycle know when it stops? we only assigned the value of the register ecx - Aleitala Ameda
  • @AleitalaAmeda will comment on the code now - Lolidze
  • @AleitalaAmeda rudely commented code - Lolidze
  • and how exactly does loop work with ecx? - Aleitala Ameda
  • 2
    @Lolidze And nothing that the TC uses the ARM processor instruction set and there are no eax registers and similar, or transition operations starting at J - Mike

For example (on a hypothetical RISC without optimization), total = 0; for(i = 0; i < 5; i=i+1) total = total + 1; total = 0; for(i = 0; i < 5; i=i+1) total = total + 1; So

 mov r0, #0 // r0: total = 0 mov r1, #5 mov LP_COUNT, r1 // loop counter (special register) mov r1, #0 // r1: i = 0 lp L1 // add r0, r0, #1 // begin_of_loop: total = total + 1 add r1, r1, #1 // i = i + 1 L1: // if --LP_COUNT != 0 goto begin_of_loop ... // first command after loop 

and if(counter > 10) then counter = 0 else counter = counter + 1

 // r0: counter cmp r0, #10 bgt L1 // counter > 10 add r0, r0, #1 // else counter = counter + 1 b L2 L1: sub r0, r0, r0 // then counter = 0 L2: 

And in some ISA (Instruction Set Architecture) there can be such "exotic"

 cmp r0, #10 movgt r0, #0 // if counter > 10 counter = 0 addle r0, r0, #1 // if counter <= 10 counter = counter + 1 

with a "dense" stream of commands without transitions (suffixes define conditional execution, the command will be executed according to the current state of the NZVC flags, which was generated in this case by the previous cmp command)

    As an option, you can see how the GCC compiler does under 64-bit Linux.

    Program code (file test.c):

     void test() { int total, i; total = 0; for(i = 0; i < 5; i=i+1) total = total + 1; } int main() { test(); return 0; } 

    Compilation:

     gcc -o test test.c 

    The result is an executable test file that can be disassembled with the command:

     objdump -M intel -d test 

    The output will be the following fragment:

     00000000004004b6 <test>: 4004b6: 55 push rbp 4004b7: 48 89 e5 mov rbp,rsp 4004ba: c7 45 fc 00 00 00 00 mov DWORD PTR [rbp-0x4],0x0 4004c1: c7 45 f8 00 00 00 00 mov DWORD PTR [rbp-0x8],0x0 4004c8: eb 08 jmp 4004d2 <test+0x1c> 4004ca: 83 45 fc 01 add DWORD PTR [rbp-0x4],0x1 4004ce: 83 45 f8 01 add DWORD PTR [rbp-0x8],0x1 4004d2: 83 7d f8 04 cmp DWORD PTR [rbp-0x8],0x4 4004d6: 7e f2 jle 4004ca <test+0x14> 4004d8: 5d pop rbp 4004d9: c3 ret 

    The first and last two instructions in assembly language are standard input and output functions, they are not interesting to you. What happens between them:

    1. The values ​​0 are assigned to the variables i and total .
    2. What exactly you are interested in (as I understood it) is an unconditional transition (jmp) to check the condition of the cycle (cmp). This is exactly the implementation of the "for loop" in assembly language. The condition is simply checked once more before performing the loop body.
    • And if you also use the -O3 flag? Or does the questioner need a strict correspondence “a separate command in C - a separate and explicit set of assembly language commands”? - ߊߚߤߘ
    • @Arhad I think already with O2 everything should come down to mov eax, 4 :) or are not the optimizers so smart? - Mike
    • one
      And by the way, why did you make an object object and disassemble it, gcc has an excellent key -S which gives an output instead of an object assembler code - Mike
    • one
      @Mike, jle vs. jl - user239133
    • 2
      For gcc to make more human code, I prefer -S -Os - avp