0

When run my assembly program keeps producing segmentation faults at the end. The program is suppose to print out the numbers from 1-100 and replace all numbers divisible by 5 and fifties by the word "Fizz". I have tried various options but I still keep getting segmentation fault. Can anyone look at my code and give me some pointers?

;program

  extern printf

SECTION .bss
    storage resd 1      ; reserve 1 dword

SECTION .data
  fmt   db "%d", 0dh,0ah,0 ; newline in formate now
  fm1   db "%s", 10,0 ; newline in formate now
  hello db "FiZZ", 0  ; the string

  count equ 0              ; output ten times

SECTION .text

global  main

main:
    push ebp
    mov ebp, esp            ; stack related

    mov eax, count
    mov ecx, 0
    mov ebx, 5
    mov dword[storage], eax ; store eax's current value


nLoop:


    mov eax, dword[storage] ; grab current count
    add eax, 1
    mov dword[storage], eax ; store new value for later

   cmp     eax, 5      ; check ecx equal to 5
   je      printFizz       ; jump to printfizz if equal to 5
   cmp     eax, 10       ; 
   je      printFizz       ; 
   cmp     eax, 15       ; 
   je      printFizz       ; 
   cmp     eax, 20       ; 
   je      printFizz       ;  
   cmp     eax, 25       ; 
   je      printFizz       ;
   cmp     eax, 30       ; 
   je      printFizz       ; 
   cmp     eax, 35      ; 
   je      printFizz       ;
   cmp     eax, 40       ; 
   je      printFizz       ; 
   cmp     eax, 45       ; 
   je      printFizz       ; 
   cmp     eax, 50       ; 
   je      printFizz       ; 
   cmp     eax, 51      ; 
   je      printFizz       ; 
   cmp     eax, 52       ; 
   je      printFizz       ; 
   cmp     eax, 53       ; 
   je      printFizz       ;
   cmp     eax, 54       ; 
   je      printFizz       ; 
   cmp     eax, 55       ; 
   je      printFizz       ; 
   cmp     eax, 56       ; 
   je      printFizz       ; 
    cmp     eax, 57      ; 
   je      printFizz       ; 
   cmp     eax, 58       ; 
   je      printFizz       ; 
   cmp     eax, 59       ;  
   je      printFizz       ; 
   cmp     eax, 60       ; 
   je      printFizz       ; 
    cmp     eax, 65      ; 
   je      printFizz       ; 
   cmp     eax, 70       ; 
   je      printFizz       ; 
   cmp     eax, 75       ; 
   je      printFizz       ; 
   cmp     eax, 80       ;
   je      printFizz       ;   
   cmp     eax, 85       ; 
   je      printFizz       ; 
   cmp     eax, 90       ; 
   je      printFizz       ; 
   cmp     eax, 95       ; 
   je      printFizz       ; 
   cmp     eax, 100       ; 
   je      EndFizz       ; 


    push eax        
    push fmt
    call printf
    add esp, 12             ; clean 3 args (4 * 3)
    mov eax, dword[storage] ; grab current count

    cmp eax, 100
    jne nLoop               ; jum to the loop 

    leave ; also stack related
    ret

printFizz:

    push    hello        ; address of ctrl string
    push    fm1         ;
    call    printf      ; Call C function 


    jmp nLoop

EndFizz:

    push    hello        ; address of ctrl string
    push    fm1         ;
    call    printf      ; Call C function 


    leave ; also stack related
    ret
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Carlos Frank
  • 167
  • 2
  • 13
  • 2
    `clean 3 args` awesome, **which** 3? You only passed 2. Also, `printFizz` doesn't clean any. Those don't cancel, unfortunately ;) Learn to use a debugger. PS: you know they probably expect you to do better than that sequence of comparisons, right? – Jester Apr 03 '16 at 23:33
  • 2
    As an addition to Jester's comments, I agree with him that you need to be concerned about restoring the stack **correctly** after making a function call when the function parameters are pushed. If you push 2 32-bit values before a printf you should be adding 8 to ESP (4*2=8), 3 DWORD values being pushed would require adding 12 (4*3=12) after. Failure to restore the stack **properly** after can lead to unusual problems and segfaults. Coincidentally I wrote another answer to a Fizz related question that mentions the stack issue as well as other things: http://stackoverflow.com/a/36092926/3857942 – Michael Petch Apr 04 '16 at 00:10
  • 20 times `cmp eax, ` `je printFizz` ? Didn't your fingers hurt from all this "ctrl-V" pressing? one principle in programming is: "never repeat yourself" – Tommylee2k Apr 04 '16 at 07:46
  • @Tommylee2k : not always true. Loop unrolling comes to mind if you are looking to break dependency chains. – Michael Petch Apr 04 '16 at 08:18
  • there's always an exception, to every rule, and most of the time you know why it's a good idea not to follow a rule; In this case it's simply nonsense to copy a branch for every single appearance ... we're just lucky the loop only goes up to 100, not 1000 or such ;-) – Tommylee2k Apr 04 '16 at 08:26

0 Answers0