0

How would I make a loop from this code I am finding it hard to make a loop of this code and I am trying to make it as short as possible using loops but I get stuck in an infinite loop. This is the original code.

      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

And this is the code where I tried to use loops to figure it out

        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

This is what I have so far with the loop but I get stuck in and infinite loop

BigMig
  • 11
  • 3
  • Short as in fewest instructions, smallest code-size (bytes of machine code), or fastest? Which one of those three are you optimizing for? Many optimizations will help with all 3, of course. e.g. `jne ask` instead of `je sum` / `jmp ask` is an obvious one. So is [`xor eax,eax` to zero RAX, instead of using a redundant REX prefix](https://stackoverflow.com/questions/33666617/what-is-the-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and). Also, why only check `ax`? An operand-size prefix is a waste of space because `scanf` returns a full 32-bit integer. – Peter Cordes Nov 05 '17 at 22:50
  • Did you try single-stepping with a debugger? If `scanf` doesn't consume characters it can't convert, so if you type something that doesn't match its format, it will get "stuck". (Also, it returns the number of successful conversions, and your format string only has one `%`, so it can never return 9.) – Peter Cordes Nov 05 '17 at 22:55
  • Wait, are you trying to use `ax` as a loop counter in the same loop where you're zeroing `al` (and the rest of `rax`) before calling variadic functions? You're clearing your loop counter every iteration, as well as using `scanf` in a way that can get stuck. – Peter Cordes Nov 05 '17 at 22:57

0 Answers0