0

I was trying to write a code that will have an array and a potr variable that will show which element you are on the array that will be edited right know. In the code after ; --- Add --- ; and ; --- Sub --- ; it adds or subtracts some value from the potrth element in the array. After ; --- Right --- ; and ; --- Left --- ; it adds or subtracts 1 from the potr variable. And after ; --- Print --- ; it prints the value of the potrth element in the array.

So I was doing things like adding some things to the first element printing it and going to the next element adding some things and printing it. But when I try to print twice it just didn't print it. Things got weirder when I try to add or subtract some stuff after printing a value, it started giving Segmentation Fault errors and printing .

For example this code,

    BITS 64 
    section .data 
    arr: times 30000 db 0 
    potr: db 0 
    output: db 0 
    section .text 
    global _start 
    _start:   
    ; --- Add --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    add rdi, 49   
    mov [arr+rax], rdi  
    ; --- Print --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]  
    mov [output], rdi   
    mov rax, 1  
    mov rdi, 1   
    mov rsi, output   
    mov rdx, 1   
    syscall   
    ; --- Right --- ;   
    mov rax, [potr]   
    add rax, 1   
    mov [potr], rax   
    ; --- Add --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    add rdi, 48   
    mov [arr+rax], rdi   
    ; --- Print --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    mov [output], rdi   
    mov rax, 1   
    mov rdi, 1   
    mov rsi, output   
    mov rdx, 1   
    syscall   
    ; --- Exit --- ;   
    mov rax, 60   
    mov rdi, 0   
    syscall 

works perfectly and writes 10.

But this code,

    BITS 64 
    section .data
    arr: times 30000 db 0 
    potr: db 0 
    output: db 0 
    section .text 
    global _start 
    _start:   
    ; --- Add --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]  
    add rdi, 49   
    mov [arr+rax], rdi   
    ; --- Print --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    mov [output], rdi   
    mov rax, 1   
    mov rdi, 1   
    mov rsi, output   
    mov rdx, 1   
    syscall   
    ; --- Sub --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    sub rdi, 1   
    mov [arr+rax], rdi   
    ; --- Print --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]  
    mov [output], rdi  
    mov rax, 1   
    mov rdi, 1   
    mov rsi, output   
    mov rdx, 1   
    syscall   
    ; --- Right --- ;   
    mov rax, [potr]   
    add rax, 1   
    mov [potr], rax   
    ; --- Add --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    add rdi, 48   
    mov [arr+rax], rdi   
    ; --- Print --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    mov [output], rdi   
    mov rax, 1   
    mov rdi, 1   
    mov rsi, output   
    mov rdx, 1   
    syscall   
    ; --- Exit --- ;   
    mov rax, 60   
    mov rdi, 0   
    syscall 

prints 1�Segmentation fault.

And if you run this code without the

    ; --- Sub --- ;   
    mov rax, [potr]   
    mov rdi, [arr+rax]   
    sub rdi, 1   
    mov [arr+rax], rdi

part it prints 10 but it should have printed 110.

So what's the problem here? Why doesn't it print twice?

Note: I'm using $ nasm -felf64 -o output.o output.asm and $ ld -o output output.o to compile.

Jester
  • 54,538
  • 4
  • 72
  • 115
Prof MYK
  • 1
  • 1
  • 1
    Use a debugger to see what is causing the fault and/or single step your program. Note that you are manipulating 8 bytes (64 bits) all over the place where you only have 1 byte (8 bits). In particular `mov rax, [potr]` loads 64 bits with presumably only the low 8 bits being valid but then you use that as an index so you get a fault. – Jester Mar 23 '22 at 18:49

0 Answers0