Hi there, I'm using YASM (which I understand to be very similar to NASM) and I was wondering if you could help me. I'm trying to write a bit of code to use bubblesort to sort a 10 character string. Here's what I've got:
arrSize equ 10
section .bss ; unititialised data
iobuf resb 1 ; used by putChar
strbuf resb arrSize
section .data ; initialized data and constants
msg db 'Enter 10 Digits: '
len equ $ - msg
msg1 db 'Sorted: '
len1 equ $ - msg1
section .text ; instructions
global _start
_start:
mov edx, len
mov ecx, msg
call putString ;print message
mov esi, '0' ;pointer
mov edi, strbuf ;base add of array
reading:
call getChar
mov [edi+1*esi], al ; store it
inc esi
cmp esi, arrSize
jl reading
mov al, 0x0A ; newline
call putChar
mov edx, len1
mov ecx, msg1
call putString
mov esi, '0' ; index = 0
mov edi, [arrSize] ; index = 0
sorting: ;edi is sorting loop index
innerLoop:
swap:
mov eax, [edi+1*esi]
cmp eax, [edi+2*esi]
jl swapend
mov eax, [edi+1*esi]
mov ebx, [edi+2*esi]
mov [edi+1*esi], ebx
mov [edi+2*esi], eax
swapend:
cmp esi, edi
jl innerLoop;
dec edi
cmp edi, 1 ;loop
jg sorting
writing:
mov al, [edi+1*esi] ; i'th byte of str
call putchar
dec esi
cmp esi, 0
jge writing
mov al, 0x0A ; newline
call putchar
mov eax, 1 ; exit()
int 0x80 ; make the system call
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
putChar: ; write a char to stdout
mov [iobuf], al ; load iobuf with al
mov edx, 1
mov ecx, iobuf
mov ebx, 1
mov eax, 4
int 0x80
mov al, [iobuf] ; restore char in al
ret
getChar: ; read a char from stdin
rawmode
mov edx, 1
mov ecx, iobuf
mov ebx, 0
mov eax, 3
int 0x80
normalmode
mov al, [iobuf] ; load al
ret
putString: ; ecx has address, edx has length
mov ebx, 1
mov eax, 4
int 0x80
ret
I'm running into a few issues. When I run, i get a segmentation fault as follows:
roger@assm -$ ./sort
Enter 10 Digits: 1234567890
Segmentation fault
roger@assm -$ 234567890
234567890: command not found
/code]