Jump to content

[ASMx86] How to move a user input array into another using Push and Pop.

Noirgheos

I'm trying to familiarize myself with procedures and push-pop in ASMx86.

 

My goal is to get the user to input a five number array using the Irvine libraries, push and pop said array into another, and have that printed out in the console. Of course, it will be printed backwards due to the nature of push-pop.

 

I'm leaving procedures for later, as I can split my code into procedures after I've actually made a working version. I'm aware that this is only feasible for a small program such as the one I'm working on.

 

Here is what I've managed so far:

 

.386
option casemap :none

include C:\Irvine32.inc			;Including libraries
include C:\Macros.inc			;Including libraries
includelib C:\Irvine32.lib		;Including libraries


.data

array1 dd 10000 dup (?)
array2 dd 10000 dup (?)
prompt1 db "Enter number: ",0
prompt2 db "Enter size of array: ",0

.code
start:
mov edx, OFFSET prompt2
call WriteString
mov ecx, eax
mov ebx, eax
loop1:
mov edx, OFFSET prompt1
call WriteString
call ReadInt
mov [array1+esi], eax
add esi,4
loop loop1

mov ecx, ebx

loop2:
push array1
pop array2
loop loop2

ret
end start						;Ends the program

This just ends with VS telling me its hit a breakpoint after I input the size of the array. Any ideas?

i7 6700K @ Stock (Yes I know) ~~~ Corsair H80i GT ~~~ GIGABYTE G1 Gaming Z170X Gaming 7 ~~~ G. Skill Ripjaws V 2x8GB DDR4-2800 ~~~ EVGA ACX 3.0 GTX 1080 SC @ 2GHz ~~~ EVGA P2 850W 80+ Platinum ~~~ Samsung 850 EVO 500GB ~~~ Crucial MX200 250GB ~~~ Crucial M500 240GB ~~~ Phanteks Enthoo Luxe

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, gabrielcarvfer said:

First thing: it seems that you forgot to read the size of the array, as the first read you do is already inside of your loop.

 

Second thing: you never initialized esi, and as it could be started with a huge offset, writing to array1+esi could try to write to another process memory, giving you a segmentation violation, that would be reported as/with a breakpoint. You should try to initialize it before using and then check if it solves your problem.

 

BTW, I made a version of what you want in NASM assembly, so you can take a look.

 

  Reveal hidden contents


%include "io.inc" ; library included in SASM IDE

section .data
    
    array1 times 2000 dd 0 ;allocate 2k 32bit integers with 0 
    array2 times 2000 dd 0 ;allocate 2k 32bit integers with 0 
    prompt1 db "Enter number: ",13,10,0 ; string, CR,LF,\0
    prompt2 db "Enter size of array: ",13,10,0 ; string, CR,LF,\0
    
section .text
global CMAIN
CMAIN:  
    pushad
      					 ; push all general purpose registers to stack
    PRINT_STRING prompt2 ; print prompt2
    GET_DEC 4, ebx       ; read 4byte decimal into ebx
    mov ecx, 0			 ; clean ecx
      
 loop1:
    PRINT_STRING prompt1    ; print prompt1
    GET_DEC 4, eax	        ; read 4byte decimal into eax
    mov [array1+4*ecx], eax ; save eax content into array1+ecx*4 offset
    inc ecx					; increase ecx
    cmp ecx, ebx 			; compare ecx and ebx
    jl loop1				; if ecx less than ebx, then loop
      
    mov edx, ecx	; save ecx to edx
    mov ebx, array1 ; move array start address to ebx
      
loop2:
    push dword[ebx] ; push array+offset contents to the stack
    add ebx,4		; add 4 to offset
    dec ecx			; decrement ecx
    jne loop2		; while ecx not zero, loop2
      
    mov ecx, edx	; load edx into ecx 
    mov ebx, array2 ; move array2 address into ebx
    
 loop3:
    pop dword[ebx]		; pop contents of the stack to ebx (array2+offset)
    PRINT_DEC 4, [ebx]  ; print the content saved to the array2+offset
    add ebx,4			; add 4 to offset
    dec ecx				; decrement ecx
    jne loop3			; while ecx not zero, loop3
    
    popad				; pop all general purpose registers from stack
    ret					; end program

 

 

If you want a pretty good assembly IDE to learn, try this one:

https://dman95.github.io/SASM/english.html

Well, I solved my breakpoints thanks to what you pointed out, but I'm still not getting the desired results. I just can't figure it out with the masm dependencies and the Irvine libraries...

i7 6700K @ Stock (Yes I know) ~~~ Corsair H80i GT ~~~ GIGABYTE G1 Gaming Z170X Gaming 7 ~~~ G. Skill Ripjaws V 2x8GB DDR4-2800 ~~~ EVGA ACX 3.0 GTX 1080 SC @ 2GHz ~~~ EVGA P2 850W 80+ Platinum ~~~ Samsung 850 EVO 500GB ~~~ Crucial MX200 250GB ~~~ Crucial M500 240GB ~~~ Phanteks Enthoo Luxe

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, gabrielcarvfer said:

Can you post your current code? And where can I find those Irvine libraries? 

 

Maybe you're missing something, maybe those called functions don't save your registers, or something like that.

 

You should check the library documentation and debug your program paying attention to registers and memory values.

http://kipirvine.com/asm/examples/Irvine_7th_Edition.msi

 

I have the libraries and inc files being used in my OP code.

 

mov edx, offset x <-- "x" is Prompt name in .data

Call WriteString           ; Prints prompt that was just offset to edx

Call Crlf                       ; Moves down a line, I put this after every Write function to make it look neat

 

Call WriteInt     ; Asks for user input and stores it in eax. 

Call WriteDec   ; Prints whatever is in eax.

 

Subsystem in VS is set to CONSOLE. Dependencies are MASM.

 

My code is unchanged other than what I fixed for the breakpoint. The console still gives me the same result of just stopping.

 

i7 6700K @ Stock (Yes I know) ~~~ Corsair H80i GT ~~~ GIGABYTE G1 Gaming Z170X Gaming 7 ~~~ G. Skill Ripjaws V 2x8GB DDR4-2800 ~~~ EVGA ACX 3.0 GTX 1080 SC @ 2GHz ~~~ EVGA P2 850W 80+ Platinum ~~~ Samsung 850 EVO 500GB ~~~ Crucial MX200 250GB ~~~ Crucial M500 240GB ~~~ Phanteks Enthoo Luxe

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, gabrielcarvfer said:

Jesus, this thing is a pain to use. Tried to use this Irvine library, the linker complains if you try to link 16-bit to 32-bit, complains if you mark the assembly as 32-bit because it's already included in the library, and etc.

 

You should read an int (ReadInt), no?

 

You need to set a breakpoint on the start and then debug instruction after instruction, checking registers and so on. I can't really help you as I couldn't even make it work here and, while SASM IDE supports MASM, it's io lib is only for NASM.

You're right, it is ReadInt.

 

And yeah, these are a pain to use. Can't imagine using them for larger programs.

 

 

i7 6700K @ Stock (Yes I know) ~~~ Corsair H80i GT ~~~ GIGABYTE G1 Gaming Z170X Gaming 7 ~~~ G. Skill Ripjaws V 2x8GB DDR4-2800 ~~~ EVGA ACX 3.0 GTX 1080 SC @ 2GHz ~~~ EVGA P2 850W 80+ Platinum ~~~ Samsung 850 EVO 500GB ~~~ Crucial MX200 250GB ~~~ Crucial M500 240GB ~~~ Phanteks Enthoo Luxe

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×