x=x+1 vs. x +=1

后端 未结 17 1251
独厮守ぢ
独厮守ぢ 2020-12-29 02:57

I\'m under the impression that these two commands result in the same end, namely incrementing X by 1 but that the latter is probably more efficient.

If this is not c

17条回答
  •  庸人自扰
    2020-12-29 03:05

    i thought the differences are due to the additional clock cycles used for memory references, but i turned out to be wrong! can't understand this thing myself

    instruction type        example                      cycles
    

    ===================================================================

    ADD reg,reg             add ax,bx                       1
    ADD mem,reg             add total, cx                   3
    ADD reg,mem             add cx,incr                     2
    ADD reg,immed           add bx,6                        1
    ADD mem,immed           add pointers[bx][si],6          3
    ADD accum,immed         add ax,10                       1
    
    INC reg                 inc bx                          1
    INC mem                 inc vpage                       3
    
    MOV reg,reg             mov bp,sp                       1
    MOV mem,reg             mov array[di],bx                1
    MOV reg,mem             mov bx,pointer                  1
    MOV mem,immed           mov [bx],15                     1
    MOV reg,immed           mov cx,256                      1
    MOV mem,accum           mov total,ax                    1
    MOV accum,mem           mov al,string                   1
    MOV segreg,reg16        mov ds,ax                       2, 3
    MOV segreg,mem16        mov es,psp                      2, 3
    MOV reg16,segreg        mov ax,ds                       1
    MOV mem16,segreg        mov stack_save,ss               1
    MOV reg32,controlreg    mov eax,cr0                     22
                            mov eax,cr2                     12
                            mov eax,cr3                     21, 46
                            mov eax,cr4                     14
    MOV controlreg,reg32    mov cr0,eax                     4
    MOV reg32,debugreg      mov edx,dr0                     DR0-DR3,DR6,DR7=11;
                                                            DR4,DR5=12 
    MOV debugreg,reg32      mov dr0,ecx                     DR0-DR3,DR6,DR7=11;
                                                            DR4,DR5=12 
    

    source:http://turkish_rational.tripod.com/trdos/pentium.txt

    the instructions may be tranlated as:

    ;for i = i+1   ; cycles
    mov  ax,   [i]  ; 1
    add  ax,   1    ; 1
    mov  [i],  ax   ; 1
    
    ;for i += 1
    ; dunno the syntax of instruction. it should be the pointers one :S     
    
    ;for i++
    inc  i          ; 3
    ;or
    mov  ax,   [i]  ; 1
    inc  ax         ; 1
    mov  [i],  ax   ; 1
    
    ;for ++i
    mov  ax,   [i]  ; 1
    ;do  stuff      ; matters not
    inc  ax         ; 1
    mov  [i],  ax   ; 1
    

    all turn out to be same :S its just some data that may be helpful. please comment!

提交回复
热议问题