0

EDIT: Thanks to Jester I got the loop to work, but the loop that is supposed to find the divisible numbers always gives me "1" and the number I'm dividing with. Am I using div wrong inside the loop?

EDIT 2:

Program working as intended, code below.

%include "io.inc"
 
extern _scanf ; externa za scanf
extern _printf; externa za printf
 
global _main
 
section .bss
N resd 1 
 
section .data
vhod db "%d", 0 ; prebrano število
izhod db "%d", 10, 0 ; izpisano število
 
section .text
_main:
    mov ebp, esp; for correct debugging
pushad ; ALPR
push dword N
push dword vhod  ;; priprava za branje
call _scanf
 
add esp, 8 ; iz esp počistimo 8 bitov
mov eax, [N] ; v eax damo N za deljenje
mov ebx, 2 ; v ebx damo 2 za deljenje
sub edx, edx ; spucamo edx, da lahko dobimo pravilni ostanek pri deljenju
div ebx ; edx:eax / ebx 
mov [N], eax ; shranimo v N dejansko število iteracij
mov ebx, 0 ; exb register nastavimo na 0
.loopf: ; začetek loopa
inc ebx ; ebx šteje kot iterator, povečamo za 1 na začetku loopa.
mov eax, ebx ; pripravimo eax kot deljenca
mov esi, 9 ; pripravimo esi kot deljitelj
sub edx, edx ; spucamo edx
div esi ; delimo
cmp edx, 0 ;pogledamo če je ostanek pri deljenju 0
jne .skip ; če ni, ne izpisemo
push ebx ; ebx dodamo na sklad
push dword izhod ; priprava za izpis
call _printf ;; izpisemo
add esp, 8  ; počistimo esp
.skip:
cmp ebx, [N], ; primerjamo N in register ebx
jl .loopf ; skok na zacetek loopa, ce je manjse 
popad ; pop klic na vse registre
ret ; return
Just a guy
  • 21
  • 4
  • 1
    What did the debugger tell you? Which line is faulting? Have you single stepped the code to see how it ends up being wrong? – Jester Dec 03 '20 at 23:45
  • Anyway, `jne .skip` skips the `push ebx` and `push dword izhod` but still hits on `add esp, 8` so unbalances the stack. Move the `add esp, 8` up to just after `call _printf`. Also `jmp .skip` is useless. – Jester Dec 03 '20 at 23:48
  • The debugger gives me the error "Program received signal SIGSEGV, Segmentation fault.". It happens at the last iteration when the loop is supposed to break. – Just a guy Dec 03 '20 at 23:50
  • I have moved the add esp, 8 up. The loop works now, thank you! Not getting the right results yet with the divisible numbers, but I'll work on that now and get back if it doesn't work. – Just a guy Dec 03 '20 at 23:54
  • @Jester I've gotten to loop to work now thanks to you, I have updated the code and tried a few fixes for the division, but have had no luck. If you happen to find some time to take a look, I'd much appreciate it. – Just a guy Dec 04 '20 at 01:04
  • 1
    You are dividing `3/ebx` not `ebx/3` as I think you intended. – Jester Dec 04 '20 at 01:08
  • That was in fact the problem, had to dig a little more into how div works so I could find a way to do it. Thank you for the help! I have updated the code above with the final solution. – Just a guy Dec 04 '20 at 02:18

0 Answers0