Author Topic: Segfault in program  (Read 17338 times)

Offline dbfn

  • Jr. Member
  • *
  • Posts: 17
Segfault in program
« on: April 14, 2010, 03:42:08 AM »
Code: [Select]
%include "macros.asm"

; %1 -> caracter a ser checado
; %2 -> limite superior
%macro check_val 2
cmp %1,00h
je %%check_end
cmp %1,'0'
jl %%invalid_value
cmp %1,%2
jg %%invalid_value
jmp %%check_end
%%invalid_value:
print invalidValue,invalidValue_l
exit 0
%%check_end:
nop
%endmacro

section .data
opening:
db "Exercicio - somador de 0 a n.", 0Ah, 0Ah
db "Digite n (de 0 a 255):"
opening_l: \
equ $ - opening
invalidValue:
db "Valor invalido!n deve estar entre 0 a 255.", 0Ah
invalidValue_l: \
equ $ - invalidValue

section .bss
n_s: resb 4
n: resb 1
res: resb 6

section .text
global main
main:
print opening,opening_l
read n_s,4
mov ebx,dword [n_s]
mov cl,4
sub cl,al
mov al,cl
mov cl,8
mul cl
mov cl,al
shl ebx,cl
mov dword [n_s],ebx
check_val byte [n_s],'2'
check_val byte [n_s+1],'5'
check_val byte [n_s+2],'5'
mov al,byte [n_s]
sub al,'0'
mov bl,100
mul bl
mov cl,al
mov al,byte [n_s+1]
sub al,'0'
mov bl,10
mul bl
add cl,al
mov al,byte [n_s+2]
sub al,'0'
add cl,al
mov al,cl
mov byte [n],al
mul cl
movzx cx,cl
add ax,cx
mov edx,0
mov ecx,2
div ecx
exit 0
Whenever I try to print and etc anything that is in the data section,the opening and opening_l beeing an exception,I get a seg fault.
WTF?
« Last Edit: April 14, 2010, 04:51:16 AM by dbfn »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Segfault in program
« Reply #1 on: April 14, 2010, 05:01:06 AM »
Works fine for me. Mac OS X (Snow Leopard/10.6.3), NASM 2.08.01, both Mach-O and ELF via OBJCONV.

However, I've rolled my own "macros.asm" that encapsulates the sys_read, sys_write, sys_exit calls.

That being said, can you post the sigificant portions of your "macros.asm", namely the print, read and write macros?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Segfault in program
« Reply #2 on: April 14, 2010, 05:43:38 AM »
Yeah, what Keith said! I "faked up" code to replace the missing macros (guessing at what they must do), and it works for me.

We really need some "context" for these questions - if you "%include "foo.inc"", tell us where we can find "foo.inc"... and whatever else we might need to duplicate the problem.

Your algorithm sucks. "199" ought to be less than 255, but it barfs because one of the digits is over 5. Is that what you intend? But it doesn't segfault...

See the links in the "Linux debug" thread for clues how to find a segfault, if any. :)

Best,
Frank


Offline dbfn

  • Jr. Member
  • *
  • Posts: 17
Re: Segfault in program
« Reply #3 on: April 14, 2010, 09:47:48 AM »
Ha...problem solved.
The problem was in the macro print.I was doing:
Code: [Select]
%macro sys_print 2
mov al,4
mov bl,1
mov edx,%2
lea ecx,[%1]
int 80h
%endmacro
Instead of:
Code: [Select]
%macro sys_print 2
mov eax,4
mov ebx,1
mov edx,%2
lea ecx,[%1]
int 80h
%endmacro
Ha...I'll correct the code...
Corrected code:
Code: [Select]
%include "sys_macros.inc"

;Macros
%macro ulj 4
cmp %1,%2
jl %4
cmp %1,%3
jg %4
%endmacro

%macro check_val 0
cmp bl,00h
je %%check_done
sub bl,'0'
ulj bl,0,9,%%invalid_value
jmp %%check_done
%%invalid_value:
sys_print invalidValue
sys_exit 0
%%check_done:
nop
%endmacro

;Program
section .data
msg1:
db "Exercicio - somador de 0 a n.", 0Ah, 0Ah
db "Digite n [0 a 999]:"
msg1_l: \
equ $ - msg1
invalidValue:
db "Digite um valor de 0 a 999!", 0Ah
invalidValue_l: \
equ $ - invalidValue
msg2:
db "Resultado da soma:"
msg2_l: \
equ $ - msg2

section .bss
n_s: resq 1
res: resb 7

section .text
global main
main:
sys_print msg1
sys_read n_s,4
mov ebx,dword [n_s]
mov dl,4
sub dl,al
mov al,dl
mov dl,8
mul dl
mov cl,al
shl ebx,cl
check_val
mov al,bl
mov cl,100
mul cl
mov dx,ax
ror ebx,8
check_val
mov al,bl
mov cl,10
mul cl
add dx,ax
ror ebx,8
check_val
mov al,bl
movzx ax,al
add dx,ax
mov ax,dx
movzx ecx,dx
mul dx
mov bx,ax
rol ebx,16
mov bx,dx
rol ebx,16
add ebx,ecx
mov edx,0
mov eax,ebx
mov ebx,2
div ebx
mov ecx,eax
mov ebx,100000
div ebx
add al,'0'
mov byte [res],al
mov eax,edx
mov edx,0
mov ebx,10000
div ebx
add al,'0'
mov byte [res+1],al
mov ax,dx
mov dx,0
mov cx,1000
div cx
add al,'0'
mov byte [res+2],al
mov ax,dx
mov cl,100
div cl
add al,'0'
mov byte [res+3],al
movzx ax,ah
mov cl,10
div cl
add al,'0'
mov byte [res+4],al
add ah,'0'
mov byte [res+5],ah
mov byte [res+6],0Ah
sys_print msg2
sys_print res,7
sys_exit 0
« Last Edit: April 14, 2010, 12:46:43 PM by dbfn »