c - NASM Assembly while loop counter -


i'm writing while loop in assembly compile in linux terminal nasm , gcc. program compares x , y until y >= x , reports number of loops @ end. here's code:

segment .data  out1    db "it took ", 10, 0 out2    db "iterations complete loop. seems lot.", 10, 0 x       db 10 y       db 2 count   db 0  segment .bss  segment .text  global main extern printf  main:     mov    eax, x     mov    ebx, y     mov    ecx, count     jmp    lp         ;jump loop lp  lp:     cmp    ebx, eax   ;compare x , y     jge    end        ;jump end if y >= x     inc    eax        ;add 1 x     inc    ebx        ;add 2 y     inc    ebx     inc    ecx        ;add 1 count     jp     lp         ;repeat loop  end:      push    out1      ;print message part 1     call    printf      push    count     ;print count     call    printf      push    out2      ;print message part 2     call    printf      ;mov    edx, out1               ;     ;call   print_string            ;                                     ;     ;mov    edx, ecx                ;these other attempts print     ;call   print_int               ;using included file                                     ;     ;mov    edx, out2               ;     ;call   print_string            ; 

this compiled , run in terminal with:

nasm -f elf test.asm gcc -o test test.o ./test 

terminal output comes out as:

it took iterations complete loop. seems lot. segmentation fault (core dumped) 

i can't see wrong logic. think it's syntactical we've started learning assembly , i've tried sorts of different syntax brackets around variables , using ret @ end of segment, nothing seems work. i've searched segmentation faults haven't found helpful. appreciated because i'm absolute beginner.

the reason crashes main function doesn't have ret instruction. sure set eax 0 signal success:

xor     eax, eax ; or `mov eax, 0` if you're more comfortable ret 

additionally, global variables designate pointers, not values. mov eax, x sets eax address of x. need write if want happen (or not use global variables).

finally, you're calling printf single non-string argument:

push    count     ;print count call    printf 

the first argument needs format string, "%i". here, count pointer null byte, nothing instead. off head, should try this:

out3    db "%i ", 0  ; snip  push    ecx push    out3 call    printf 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -