12 Replies - 1990 Views - Last Post: 30 September 2013 - 07:25 PM

#1 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Using RadASM how do I prevent the console window from exiting?

Posted 28 September 2013 - 06:20 PM

Hi I'm trying to write a assembly program but every time I try to run it and make sure it is outputting the right results it exits before I even get a chance to look at it. I can't email my teacher because he doesn't actually own a computer(apparently he never has) so he won't answer until Monday and I meant to ask him on Friday but apparently he left early without letting us know so I didn't get to ask him then either. So if anyone can help that'd be great. Here's the code and as mentioned in the topic name I'm using RadASM if that helps at all.
Here is my code in case you need it. BTW this is unfinished code I'm just trying to make sure I'm inputting the values into the array correctly and whether it stores negative numbers or not. Anyway, thanks in advance for any help given.

.586
.model flat

ExitProcess proto stdcall, dwExitCode:DWORD ; function prototype

public _start	 ; entry point made available to the linker

include ../io.h 	; header file for input/output

;.stack  4096           ; reserve 4096-byte stack

CR    = 0Dh
LF    = 0Ah
NULL  = 0

.data
	message1     byte "Enter next integer, or 999 to quit: ", NULL
	msgDone      byte "Done!", NULL
	CRLF         byte 0Dh,0Ah, NULL;
	inputString  byte 40 dup(?)
	outputChar   byte ?, NULL
	intOut       byte 10 dup(?)
	array        dword 100 dup(?)
	min          dword ?
	max          dword ?
	numVls       dword ?
	minDone      dword 0
	maxDone      dword 0
	sumDone      dword 0
	avgDone      dword 0
	posChgDone   dword 0
	negChgDone   dword 0
	devDone      dword 0
	grdsDone     dword 0
	intPrint     dword ?
.code

_start:
	
main proc
	output CRLF
	mov    eax, 0
	mov    ecx, 0                              ; running count values entered
	mov    edi, offset array                   ; points to array element intArray[0]
	
enterNextNumber:
	output CRLF
	output message1
	input  inputString, 40
	atod   inputString
	cmp    eax, 999
	je     setNumVls
	mov    [edi], eax
	add    edi, 4
	inc    ecx
	jmp    enterNextNumber
setNumVls:
	mov    numVls, ecx
	jmp    resetArray
resetArray:
	mov    edi, offset array
	mov    ecx, 0
	jmp    printArray
printArray:
	output CRLF
	mov    eax, [edi]
	mov    intPrint, eax
	dtoa   intPrint, eax
	output intPrint
	output CRLF
	add    edi, 4
	cmp    ecx, numVls
	je     finished
	inc    ecx
	jmp    printArray
	
	
finished:
	output CRLF
	output msgDone
	;invoke ExitProcess, 0



Is This A Good Question/Topic? 0
  • +

Replies To: Using RadASM how do I prevent the console window from exiting?

#2 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 28 September 2013 - 09:39 PM

Ok so I decided to keep trying to finish the program and I start with finding the min and max in the program because it seems easy. However, no matter what I do I can't even get the min to work. Haven't even tried finding the max yet. Here is what I have so far:
.586
.model flat

ExitProcess proto stdcall, dwExitCode:DWORD ; function prototype

public _start	 ; entry point made available to the linker

include ../io.h 	; header file for input/output

;.stack  4096           ; reserve 4096-byte stack

CR    = 0Dh
LF    = 0Ah
NULL  = 0

.data
	message1     byte "Enter next integer, or 999 to quit: ", NULL
	msgDone      byte "Done!", NULL
	CRLF         byte 0Dh,0Ah, NULL;
	inputString  byte 40 dup(?)
	outputChar   byte ?, NULL
	intOut       byte 10 dup(?)
	array        dword 100 dup(?)
	min          dword ?
	max          dword ?
	numVls       dword ?
	minDone      dword 0
	maxDone      dword 0
	sumDone      dword 0
	avgDone      dword 0
	posChgDone   dword 0
	negChgDone   dword 0
	devDone      dword 0
	grdsDone     dword 0
	intPrint     dword ?
	print2       dword ?
	

.code

_start:
	
main proc
	output CRLF
	mov    eax, 0
	mov    ecx, 0                              ; running count values entered
	mov    edi, offset array                   ; points to array element intArray[0]

enterNextNumber:
	output CRLF
	output message1
	input  inputString, 40
	atod   inputString
	cmp    eax, 999
	je     setNumVls
	mov    [edi], eax
	add    edi, 4
	inc    ecx
	jmp    enterNextNumber
setNumVls:
	mov    numVls, ecx
	;mov    eax, numVls
	;mov    print2, eax
	;dtoa   print2, eax
	;output print2
	jmp    done1
done1:
	mov    edi, offset array
	mov    ebx, [edi]
	mov    min, ebx
	;jmp    printArray
	jmp    findMin
nextProcess:
	jmp printMin
findMin:
	add edi, 4
	cmp min, edi
	jl setMin
	inc ecx
	cmp ecx, numVls
	je setMinDone
	jmp findMin
setMin:
	mov min, 0
	mov min, edi
	cmp ecx, numVls
	je setMinDone
	jmp findMin
setMinDone:
	mov minDone, 1
	jmp nextProcess
	
	
printMin:
	output CRLF
	mov esi, min
	mov intPrint, esi
	dtoa intPrint, esi
	output intPrint
;printArray:
	;output CRLF
	;mov    eax, [edi]
	;mov    intPrint, eax
	;dtoa   intPrint, eax
	;output intPrint
	;output CRLF
	;add    edi, 4
	;cmp    ecx, numVls
	;je     finished
	;inc    ecx
	;jmp    printArray
	
	
finished:
	output CRLF
	output msgDone
	invoke ExitProcess, 0

main endp



The commented out parts were used for debugging purposes. Anyway, when I just printed the min when it was set to the first value it worked but when I added the parts that go through the array again and check for any values less than the first value and try to print out the result it doesn't print out the right value. I don't know what it's printing out because the program exits before I can see it but I know it isn't the right value because it's some really large number and I only enter the values 1, 2, 3, 4 and 999 to quit. So, what am I doing wrong? BTW I will probably need help with other parts of the program as well so stay tuned to this topic and please help me out because even if I could email my teacher and get a response he doesn't explain things very well and the book isn't very helpful either. Thanks again for any and all help.
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 28 September 2013 - 11:31 PM

Well, the console is acting the way it should - it closes once the program is finished! To see any output, you could run your program right from the cmd window - open a command prompt and cd to the directory your program is in and run your program, you will see the output.

I believe the io code uses masm32 so you should have access to its functions and macros, so right before your call to ExitProcess, just add inkey "Press any key to quit" or you could link to the C runtime msvcrt and call getch.

I do not have the io.obj so I cannot Assemble and link your code, so I used the macros and functions from MASM32, I kept it as "High Level" as I can to keep it clear:

First we get entered numbers and add to array, we exit loop if user entered 999 or loop counter == 999 since you define your array to hold 100 dwords, index 0 - 999

Then we need to sort the array, use whatever algo you want a bubble sort is good for this simple array. I will leave the sort to you

include masm32rt.inc
include msvcrt.inc
CR    = 0Dh 
LF    = 0Ah
NULL  = 0

.data
	message1     byte "Enter next integer, or 999 to quit: ", NULL
	szPressKey      db  "Press any key to quit", 0
	
	msgDone      byte "Done!", NULL
	CRLF         byte 0Dh,0Ah, NULL;
	inputString  dword ?;byte 40 dup(?)
	outputChar   byte ?, NULL
	intOut       byte 10 dup(?)
	array        dword 100 dup(?)
	min          dword ?
	max          dword ?
	numVls       dword ?
	minDone      dword 0
	maxDone      dword 0
	sumDone      dword 0
	avgDone      dword 0
	posChgDone   dword 0
	negChgDone   dword 0
	devDone      dword 0
	grdsDone     dword 0
	intPrint     dword ?
	print2       dword ?
	

.code

_start:
	
main proc
	print   offset CRLF
	mov     edi, offset array   ; points to array element intArray[0]

    xor     ebx, ebx            ; loop counter
enterNextNumber:
	print   offset message1
	invoke  atodw, input() 
	cmp     eax, 999            ; do we exit?
	je      SortArray
	mov     [edi + 4 * ebx], eax
	inc     ebx
	cmp     ebx, 999            ; are we at max array elems?
	jne     enterNextNumber     ; no, continue getting numbers
	; user entered max array elems, fall through to SortArray

SortArray:
    ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ; for tetsting, print unsorted array
    mov     esi, ebx            ; save count of entered numbers
    xor     ebx, ebx
PrintIt:
    mov     eax, [edi + 4 * ebx]
    print   ustr$(eax)
    print   offset CRLF
    inc     ebx
    cmp     ebx, esi            ; did we print enough:
    jne     PrintIt
    ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
finished:
    print   offset szPressKey
	call    crt__getch
	invoke  ExitProcess, 0

main endp

end _start


Once you sort the array, the min will be array[0] and max will be array[numbers entered - 1]

after adding a Comb Sort:
Attached Image
Was This Post Helpful? 2
  • +
  • -

#4 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 29 September 2013 - 09:39 AM

What would a sorting algorithm require in assembly because the only instructions we've learned are: mov, cmp, all the je, jle, ect, imul, idiv, add, sub and input and output. We haven't learned how to use the ebp and esp because he keeps saying we don't need to know that right now so would sorting require anymore than that? Also what is xor?

This post has been edited by HKCHaN92: 29 September 2013 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

#5 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 29 September 2013 - 07:08 PM

Ok, so I waited to do the sorting algorthim last and I know this probably sucks and is totally wrong but it's the best I could do after working on this program all day today and most of the day yesterday but here is the bubble sort algorithm I have so far.
sort proc
	
outerLoop:
	mov ecx, 0
	mov edi, offset array
compare:
	mov ebx, 0
	mov edx, 0
	mov ebx, [edi + 4 * ecx]
	inc ecx
	mov edx, [edi + 4 * ecx]
	cmp ebx, edx
	jg swap
continue:
	inc ecx
	add edi, 4
	cmp ecx, n
	je outerLoop
	jmp compare
	
jmp finished
swap:
	mov temp2, ebx
	mov ebx, edx
	mov edx, temp2
	mov [edi + 4 * ecx], ebx
	dec ecx
	mov [edi + 4 * ecx], ebx
	jmp continue
finished:
	ret

sort endp


I don't know if it's lack of sleep, or just mental exhaustion from working on this program all day or both but I can't seem to figure out what condition I need to check before jmp finished and there are probably lots of other errors in there too. If you could look at and help me fix it I would greatly appreciate it this is the last thing (hopefully, unless further tests prove otherwise) that I need to do before this program is finished and I can move on to my Java program that is due next monday and hopefully finish it early enough so I can go home next weekend and spend time with my family. Anyway, any and all help is appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 29 September 2013 - 08:15 PM

The Bubble Sort is much simpler than that! Some times the best way to figure something out is with pencil and paper. Write down a sample array and use pseudo code to describe the steps need to sort the array.

Let's see the array after one iteration through the loop, comparing and swapping:
Attached Image

After the first iteration, the array is still not sorted, but the largest value is where it should be. We now continue the loop comparing and swapping and keep doing this n - 1 iterations, then the array is sorted.

This is a simple Bubble Sort, I added a "bug" in that it sorts the array in descending order instead of ascending order. It is up to you to figure out what to change to make it sort in ascending order.

BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD, Count:DWORD			
;
; Sort an array of 32-bit signed integers in ascending order
; using the bubble sort algorithm.
; Receives: pointer to array, array size
; Returns: nothing
;-----------------------------------------------------------

	mov     ecx, Count
	dec     ecx			    ; decrement count by 1

OuterLoop:	
    push    ecx			    ; save outer loop count
	mov     esi, pArray		; point to first value

Compare:	
    mov     eax, [esi]		; get array value
	cmp     [esi + 4], eax	; compare a pair of values
	jl      NoExchange		; if [esi] <= [edi], don't exch
	xchg    eax, [esi + 4]	; exchange the pair
	mov     [esi], eax

NoExchange:	
    add     esi, 4		    ; move both pointers forward
	loop    Compare		    ; inner loop

	pop     ecx		        ; retrieve outer loop count
	loop    OuterLoop		; else repeat outer loop

Done:
    ret
BubbleSort ENDP

Was This Post Helpful? 2
  • +
  • -

#7 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 30 September 2013 - 08:56 AM

Ok so I changed it but it still stops working before it can even finish one iteration. Here is the code and the code that calls the sort and prints array.
;IN MAIN
resetArray:
	mov edi, offset array
	mov ecx, 0
	mov eax, 0
callSort:
	push offset numVls
	push offset array
	output CRLF
	output tstMsg2
	call sort
	output CRLF
	output tstMsg2
printArray:
	output CRLF
	mov eax, [edi+4*ecx]
	mov intPrint, eax
	dtoa intPrint, eax
	push offset intPrint
	call stripBlanks
	output intPrint
	output CRLF
	cmp ecx, numVls
	je  Grades
	inc ecx
	jmp printArray


sort proc
	mov ecx, numVls
	mov edi, offset array
	dec ecx 
	output CRLF
	output tstMsg2
outerLoop:
	push ecx                 
	mov edi, array
	output CRLF
	output tstMsg3          ;stops working after this point
compare:
	mov eax, [edi]
	cmp [edi + 4], eax
	jl  noSwap
	xchg eax, [edi+4]
	mov  [edi], eax
	output CRLF
	output tstMsg4
noSwap:
	add edi, 4
	jmp compare
	pop ecx
	jmp outerLoop
	output CRLF
	output tstMsg1
finished:
	pop ecx
	output CRLF
	output tstMsg2
	ret	
sort endp


Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 30 September 2013 - 04:13 PM

Please tell me your teacher is NOT teaching you Assembly and not teaching you procedures with parameters. They also are NOT really teaching you to use all these global variables!

Let's look at my code (On the left) and your code, side by side:

                           ;	mov ecx, numVls
	mov     ecx, Count     ;	mov edi, offset array
	dec     ecx			   ;	dec ecx 
                           
OuterLoop:	               ;outerLoop:
    push    ecx			   	;   push ecx                 
	mov     esi, pArray	   	;   mov edi, array
                           	
Compare:	               ;compare:
    mov     eax, [esi]	   	;   mov eax, [edi]	
	cmp     [esi + 4], eax 	;   cmp [edi + 4], eax
	jl      NoExchange	   	;   jl  noSwap	
	xchg    eax, [esi + 4] 	;   xchg eax, [edi+4]
	mov     [esi], eax     	;   mov  [edi], eax
                           	
NoExchange:	               ;noSwap:
    add     esi, 4		   	;   add edi, 4
	loop    Compare		   	;   jmp compare
                           	
	pop     ecx		       	;pop ecx
	loop    OuterLoop	   	;   jmp outerLoop
                           	
Done:                      ;finished:
    ret                    	;   pop ecx
                        	;   ret	




Using different registers in your code is not a big deal, BUT registers are named according to their usage which makes it easier to understand when looking at code. esi is the source register which is used with strings, edi is the destination register, but no biggie.

Do you notice extra instructions in your code and different instructions in the loops? (well technically you have two loops but they are infinite)

I used mov esi, pArray because I passed the address of the array to the procedure and going by Hungarian notation, the p before array stands for pointer. You are not passing any parameter to your sort function, but use correct code once, then incorrect after.

Since you are not passing addresses, you need to use offset before array like you did on line 2 mov edi, offset array

In the beginning of OuterLoop, replace mov edi, array with mov edi, offset array and get rid of your line 2.

Why are you popping ecx before the ret? You are screwing up the stack with it. Oh, and that bug I left in there is still there!

Your code:
callSort:
	push offset numVls
	push offset array
	output CRLF
	output tstMsg2
	call sort


Why are you pushing those two things if you are not using parameters? If you push them, pop them or the stack pointer gets out of whack!
Was This Post Helpful? 2
  • +
  • -

#9 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 30 September 2013 - 06:13 PM

Ok I made the changes and fixed it and it works now. But the thing is we haven't learned the loop instruction yet so I'm not sure if he'll be ok with that or not but we haven't learned xchg either and I'm probably keeping that. Also I was working on the sort funciton in between classes and I posted that when it was almost time for my next class to start and I still had to get across campus to get there so I forgot to change jl noSwap to jg noSwap so sorry about that. Also we haven't gotten to functions yet so I was just going off one of the examples in the book and didn't know that you only did the offset thing with parameters so sorry about that as well. Thanks so much for your help. Also I was wondering once I've cleaned it up a bit could you look at my code sometime 2mrrw if I send it to you by private message with the io.obj file (if it lets me attach them) and io.h. I understand if you don't want to or you don't have time but I'm still really new at this and our teacher hasn't gone over a whole lot because he gets sidetracked really easily so he ends up talking about stuff that isn't important and before he realizes it class is over. Anyway I greatly appreciate your help and your patience.

This post has been edited by GunnerInc: 30 September 2013 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#10 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 30 September 2013 - 06:27 PM

Apparently I spoke too soon I just tested with a different set of values and it gave me the wrong min and max I probably just have something set wrong or something like that I'll figure it out. Anyway thanks again for all your help.
Was This Post Helpful? 0
  • +
  • -

#11 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 30 September 2013 - 06:30 PM

Loop using the loop instruction:

    mov     ecx, 10             ; loop iterations
Print1:
    push    ecx
    print   ustr$(ecx)
    pop     ecx
    loop     Print1


Loop without the loop instruction:

    mov     ecx, 10
Print2:
    push    ecx
    print   ustr$(ecx)
    pop     ecx
    dec     ecx
    jnz     Print2  


Internally, the loop instruction does the dec and jnz for you.

xchg swaps the two regsiters, how would you do that without using xchg?
Was This Post Helpful? 1
  • +
  • -

#12 HKCHaN92   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 30 September 2013 - 06:34 PM

Using temp value to move a value that is in a register into temp value then move value from another register into that register and then move the value in temp into the second register.
Was This Post Helpful? 0
  • +
  • -

#13 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Using RadASM how do I prevent the console window from exiting?

Posted 30 September 2013 - 07:25 PM

No need to quote the previous posts in your replies.

Your close with the temp value idea, just use another register.

From the INTEL manual for xchg:

Quote

Exchanges the contents of the destination (first) and source (second) operands


TEMP <-- DEST
DEST <-- SRC
SRC <-- TEMP

So, this:
xchg    eax, [esi + 4]

can be rewritten as:
	mov     edx, eax        ; TEMP <-- DEST
	mov     eax, [esi + 4]  ; DEST <-- SRC
	mov     [esi + 4], edx  ; SRC <-- TEMP

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1