LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why segment fault? (https://www.linuxquestions.org/questions/programming-9/why-segment-fault-324982/)

andrewnow 05-19-2005 07:18 AM

why segment fault?
 
the code i wrote is to find the biggest number in num,but it shows segment fault when i run it.

nasm -f elf and.asm -o and.o
gcc -o and.o
./and
segment fault
////////////////////////////////////
section .data
num db -19,28,37,-46,55,61,-74,88
count equ $-num
max db 'max=?',0dh,0ah,'$'

section .text
global main
main:
mov al,0
mov ebx,num
mov ecx,[count]
last:
cmp [ebx],al
jc next
mov al,[ebx]
next:
inc ebx
loop last
mov [max+4],al
mov eax,4
mov ebx,1
mov ecx,max
mov edx,8
int 80h
mov eax,1
int 80h

PTrenholme 05-19-2005 07:40 AM

I can't help with your seg fault problem, but your initalization seems wrong. You've started with a value of zero, and -- if your data was all less than zero -- your logic would return zero instead of the largest value.

Also, I think you're comparing the character byte values rather than the numeric values represented by the set of bytes. (I may be wrong on this point -- its been thirty years since I last did asembly programming, and that was on an IBM 3090.)

andrewnow 05-19-2005 08:16 AM

thank you very much Mr. PTrenholme. your suggestion seems much vulable. I'll try again. thank you again.


All times are GMT -5. The time now is 04:52 PM.