problems with assembly

view full story

http://www.daniweb.com – Hi, I'm a student in assembly. I'm using NASM and running Ubuntu Linux. For our assignment we need to use subprograms, loops, stacks, and arrays. We are supposed to get a positive integer, find all the factors, put each factor onto an array, load the array onto the stack, then output the factors in decreasing order from original positive integer to 1. Unfortunately, the tutors at my school are clueless about assembly and even my teacher is frustrated with assembly and refuses to look at code. I'm having several problems. Currently, I get a return value of 1, which could be the factor 1 or the index of the array. I understand loops, but I'm lost with stacks and arrays. Also, the program runs in an infinite loop, how can I solve this? welcome db "Welcome to Randy Brummett's friendly factorization program", 0 prompt db "Please enter a positive integer: ", 0 output db "The factors of ", 0 are db " are ", 0 comma db ", ", 0 repeat db "Do you have more inputs (Y or N)? ", 0 bye db "Have a nice day. Bye.", 0 ; ; Uninitialized data are placed in the .bss segment ; segment .bss ; ; ; ======= Place uninitialized data here ; myint resd 1                                        ; positive integer to be factored divisor resd 1                                        ; divisor factorarray resd 34                                ; array for factors ; ; Code is put into the .text segment ; segment .text       global  asm_main                        ; declare _asm_main if compiling on Windows asm_main:                                ; delcare _asm_main if compiling on Windows       enter    0,0                    ; setup routine       pusha                            ; push all registers on the system stack ; ; Begin execution area ; ; ; ; ; ======== Place executing instructions here ; ; mov [ebx], factorarray                        ; ebx = address of factorarray call getinput                                ; getinput subprogram call factorization call displayfactors                        ; displayfactors subprogram getinput:                                ; begin subprogram mov eax, welcome                        ; displays welcome message call print_string                        ; print prompt call print_nl                                ; new line mov eax, prompt                                ; prompts for input call print_string                        ; print prompt call read_int                                ; read integer mov [myint], eax                        ; store into int ret                                        ; end subprogram factorization:                                ; begin subprogram mov dword [divisor], myint                ; store integer into divisor mov dword [factorarray], divisor        ; put positive integer as a factor in array findfactors:                                ; begin loop cmp dword [divisor], 1                        ; if divisor = 1 quit, else continue jz endif                                ; quit sub dword [divisor], 1                        ; divisor - 1 mov eax, [myint]                        ; put dividend into eax mov ecx, [divisor]                        ; put divisor into ecx mov edx, 0                                ; initialize edx to 0 div ecx                                        ; divide cmp edx, 0                                ; if remainder = 0 add integer into array, else check next divisor jz addarray                                ; add divisor to array jnz findfactors                                ; check other divisors if they are factors loop findfactors                        ; end loop addarray:                                ; begin addarray mov eax, [divisor]                        ; put divisor into eax mov dword [factorarray], eax                ; store factor into array add ebx, 4                                ; increment array jmp findfactors                                ; jump to findfactors to find more factors endif:                                        ; quit subprogram push dword factorarray                        ; push array onto stack pop eax                                        ; pop array so no seg fault ret                                        ; end subprogram displayfactors: mov eax, output                                ; display output message call print_string                        ; print mov eax, [myint]                        ; display positive integer from user call print_int                                ; print mov eax, are                                ; display message "are" call print_string                        ; print mov eax, [factorarray] ret                                        ; end subprogram (General)