x86使用循环以最小化代码(衬里)

时间:2017-11-05 21:53:08

标签: loops optimization x86 x86-64 inline-assembly

如何从这段代码中创建一个循环我发现很难对这段代码进行循环,我试图尽可能地使用循环,但是我陷入了无限循环。 这是原始代码。

      section .rodata                                                                                                                                                                                 
      prompt1    db "Please enter a number: ",0   ; 0 is null character                                                                                                                               
      prompt2    db "Enter another number: ",0                                                                                                                                                        
      format_str db "The sum is: %ld.",10,0  ; 10 is LF                                                                                                                                               
      num_format db "%ld",0                                                                                                                                                                           

      section .text                                                                                                                                                                                   
      global main              ; main and _start are both valid entry points                                                                                                                          
      extern printf, scanf     ; these will be linked in from glibc                                                                                                                                   

      main:                                                                                                                                                                                           
      ; prologue                                                                                                                                                                                      
      push    rbp          ; save base pointer to the stack                                                                                                                                           
      mov     rbp, rsp     ; base pointer = stack pointer                                                                                                                                             
      sub     rsp, 80      ; make room for integers on the stack                                                                                                                                      
      push    rbx          ; push callee saved registers onto the stack                                                                                                                               
      push    r12          ; push automatically decrements stack pointer                                                                                                                              
      push    r13                                                                                                                                                                                     
      push    r14                                                                                                                                                                                     
      push    r15                                                                                                                                                                                     
      pushfq               ; push register flags onto the stack                                                                                                                                       

      ;move   rdx, 0                                                                                                                                                                                  
      sub    rbp, 8                                                                                                                                                                                   
      ; prompt for first integer                                                                                                                                                                      
      mov    rdi, dword prompt1    ; double word is 4 bytes; a word is 2 bytes                                                                                                                        
      xor    rax, rax              ; rax is return value register - zero it out                                                                                                                       
     call   printf                ; call the C function from glibc                                                                                                                                   
      lea    rsi, [rbp]          ; load effective address - this instruction                                                                                                                          
      mov    rdi, dword num_format ; load rdi with address to format string                                                                                                                           
      xor    rax, rax              ; zero out return value register                                                                                                                                   
      call   scanf                 ; call C function                                                                                                                                                  
      ; scanf reads the input as an integer                                                                                                                                                           

      sub    ax, ax                                                                                                                                                                                   
      ;mov    rdx, 0                                                                                                                                                                                  
      ;mov    rdx, 0                                                                                                                                                                                  

      jmp ask                                                                                                                                                                                         

      ask:                                                                                                                                                                                            

      add ax, 1                                                                                                                                                                                       
      sub rbp, 8                                                                                                                                                                                      

      ; prompt for other integer                                                                                                                                                                      
      mov    rdi, dword prompt2                                                                                                                                                                       
      xor    rax, raxlea    rsi, [rbp]         ; read number 
      call   printf                                                                                                                                                                                   
      lea    rsi, [rbp]                                                                                                                                                        
      mov    rdi, dword num_format                                                                                                                                                                    
      xor    rax, rax                                                                                                                                                                                 
      call   scanf                                                                                                                                                                                    

      cmp ax, 9                                                                                                                                                                                       
      je sum                                                                                                                                                                                          

      jmp ask                                                                                                                                                                                         

      sum:                                                                                                                                                                                            

     ; add all numbers together                                                                                                                                                                      
     xor rbx, rbx            ; sum = 0                                                                                                                                                               
     mov rcx, [rbp-8]        ; RBX = number                                                                                                                                                          

     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-16]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-24]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-32]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-40]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-48]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-56]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-64]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-72]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rcx, [rbp-80]       ; RBX = number                                                                                                                                                          
     add rbx, rcx            ; add num + num - store in rbx                                                                                                                                          
     mov rsi, rbx                                                                                                                                                                                    
     mov rdi, dword format_str                                                                                                                                                                       
     xor rax, rax            ; rax is return value register - zero it out                                                                                                                            
     call printf             ; call the C function from glibc                                                                                                                                        
     exit:                                                                                                                                                                                           
     ; epilogue                                                                                                                                                                                      
     popfq                                                                                                                                                                                           
     pop     r15                                                                                                                                                                                     
     pop     r14                                                                                                                                                                                     
     pop     r13                                                                                                                                                                                     
     pop     r12                                                                                                                                                                                       
     pop     rbx                                                                                                                                                                                     
     add     rsp, 80       ; set back the stack level                                                                                                                                                
     leave                                                                                                                                                                                           
     ret

这是我尝试使用循环来解决问题的代码

        ask:                                                                                                                                                                                            

      add ax, 1                                                                                                                                                                                       
      sub rbp, 8                                                                                                                                                                                      

      ; prompt for other integer                                                                                                                                                                      
      mov    rdi, dword prompt2                                                                                                                                                                       
      xor    rax, rax                                                                                                                                                                                 
      call   printf                                                                                                                                                                                   
      lea    rsi, [rbp]         ; read number                                                                                                                                                         
     mov    rdi, dword num_format                                                                                                                                                                    
    xor    rax, rax                                                                                                                                                                                 

     call   scanf                                                                                                                                                                                    

     cmp ax, 9                                                                                                                                                                                       
     je sum                                                                                                                                                                                          

      jmp ask

这是我到目前为止所使用的循环但是我陷入了无限循环

0 个答案:

没有答案