Real Donald Trump
Real Donald Trump

Reputation: 422

Why I got a segmentation fault?

I am new to assembly programming. I'm trying to make a simple program in x64 assembly to print an inverted triangle to console. I expect my program to output something like this:

********
 *******
  ******
   *****
    ****
     ***
      **
       *

This is my effort so far:

;--------------------------------------
; compile and run:
; $ nasm -f elf64 invtriangle.asm
; $ ld -s -o invtriangle invtriangle.o
; $ ./invtriangle
;--------------------------------------

                bits 64
                global _start

                section .text
_start:
                mov     r8, 0          ; copy maximum number of whitespace characters allowed for first line to register 'r8'
                mov     r9, 8          ; copy maximum number of stars allowed for first line to register 'r9'
                mov     r10, 0         ; number of whitespace characters written on line so far
                mov     r11, 0         ; number of star characters written on line so far
                mov     rbx, output    ; copy address of the string pointer to register 'rbx'
writeline:
writewhitespace:
                cmp     r8, 0          ; check if the value of register 'r8' is zero
                je      writestars     ; if so, skip writing whitespaces
                mov     byte[rbx], ' ' ; write a whitespace character
                inc     rbx            ; advance pointer to next address to write
                inc     r10            ; increment number of whitespace characters written on line so far by 1
                cmp     r10, r8        ; compare register values: check if we reached the maximum number of whitespace characters allowed for the current line
                jne     writewhitespace; if not, continue writing whitespaces
writestars:
                mov     byte[rbx], '*' ; write s star
                inc     rbx            ; advance pointer to next address to write
                inc     r11            ; increment number of star characters written on line so far by 1
                cmp     r11, r9        ; compare register values: check if we reached the maximum number of star characters allowed for the current line
                jne     writestars     ; if not, continue writing stars
lineend:
                mov     byte[rbx], 10  ; write a new line character (ascii value for new-line is 10)
                inc     rbx            ; advance pointer to next address to write
                inc     r8             ; the next line will be one whitespace longer
                dec     r9             ; the next line will be one star shorter
                mov     r10, 0         ; reset the counter (number of whitespace characters written on line)
                mov     r11, 0         ; reset the counter (number of star characters written on line)
                cmp     r8, maxlines   ; did we exceed the maximum number of lines?
                jng     writeline      ; if not, continue writing lines
                mov     rax, 1         ; system call for write (64 bit)
                mov     rdi, 1         ; file descriptor = stdout
                mov     rsi, output    ; copy the address of string 'output' to output
                mov     rdx, nbytes    ; copy number of bytes in output
                syscall                ; invoke OS to write
                mov     rax, 60        ; system call for exit (64 bit)
                mov     rdx, 0         ; exit status = 0
                syscall                ; invoke OS to exit

                section .bss
maxlines        equ     8              ; maximum number of lines to write
nbytes          equ     72             ; (8 + 8 + 8 + 8 + 8 + 8 + 8 + 8) + 8 = 72 bytes (don't forgot to take account of new-line characters!)
output          resb    nbytes         ; initialize a string pointer by reserving number of bytes

When I run the assembly program above I kept getting a segmentation fault and I can't figure out the cause of it. I tried to change registers and I got the same result. Interestingly when I increase the maximum number of stars allowed for first line from 8 to 10 in register r9, the program ran without segmentation fault but the output wasn't what I expected (I don't know why it happened). I'm not sure if I calculated the variable nbytes correctly in bss section. I will appreciate any form of assistance, thanks!

I am using 64-bit Ubuntu OS to run the program above.

Upvotes: 3

Views: 182

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 57922

Hint: What happens to your writestars loop at the last line, when zero stars are to be printed (so that r9 is 0)?

Upvotes: 1

Related Questions