I am building a compiler for a mini programming language. I happened to stumble on a head-scratching bug.
; & nasm -f elf64 debug.asm && gcc -m64 -no-pie -o debug debug.o && ./debug
bits 64
global main
extern printf
section .text
main:
push rbp
xor ebx, ebx
add byte[block + ebx], 10 ; [block + ebx] = 10
; [block + ebx + 1] += [block + ebx] * 7
mov rax, [block + ebx]
imul rax, 7
add byte[block + ebx + 1], al
; [block + ebx] = 17930??? why?!?
mov rdi, fmt
mov rsi, [block + ebx]
call printf
add ebx, 1
mov rdi, fmt
mov rsi, [block + ebx]
call printf
pop rbp
mov rax, 60
xor rdi, rdi
syscall
section .data
block times 30000 db 0 ; array of bytes
fmt: db "%lld", 10, 0
When I run the program above. I noticed that the value inside [block + ebx]
abruptly changed from 10 to 17930 after addition. I don't know why it happened. I suspect it's an integer overflow. Any ideas? How can I fix it. Thanks in advance.