0

Hello guys,
I wrote this program to encrypt a string. It uses the WriteConsoleA function to print output, and the ReadConsoleA function for the input. I have called all the functions using stack. Here is the code:

.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

extrn GetStdHandle@4 : PROC
extrn WriteConsoleA@20 :PROC
extrn ReadConsoleA@20 : PROC

.data
prompt BYTE "Please Enter Your name:", 13, 10 ,0
prompt1 BYTE "Encrypted Name is:", 13, 10 ,0 

.data?
 buf BYTE 100 DUP(?)
 inp BYTE 100 DUP(?)
 ln DWORD ?

   .code
    main PROC
                ;pushing arguments into stack for function GetStdHandle
                mov eax, STD_OUTPUT_HANDLE
                push eax
                CALL GetStdHandle@4


                ;WriteConsoleA
                mov ebx, 0
                push ebx
                mov ebx, OFFSET buf
                push ebx
                mov ebx, LENGTHOF prompt
                push ebx
                mov ebx, OFFSET prompt
                push ebx
                push eax
                CALL WriteConsoleA@20 





                ;push arguments for getstdhandle
                mov eax, STD_INPUT_HANDLE
                push eax
                CALL GetStdHandle

                ;push arguments for ReadConsoleA
                mov ebx, OFFSET buf
                push ebx
                mov ebx, 0
                push ebx
                mov ebx, 100
                push ebx
                lea ebx, inp
                push ebx
                push eax
                CALL ReadConsoleA@20

                ;call StripLF
                lea ebx, inp
                push ebx
                CALL StripLF

                ;Start Encryption
                mov ecx, SIZEOF inp
                mov esi, 0
                L1:
                        xor inp[esi], 11101101b
                        inc esi
                        loop L1

                ;Print Cipher Text:        

                ;GetStdHandle
                mov eax, STD_OUTPUT_HANDLE
                push eax
                CALL GetStdHandle

                ;WriteConsoleA
                mov ebx, 0
                push ebx
                mov ebx, OFFSET ln
                push ebx
                mov ebx, LENGTHOF inp
                push ebx
                lea ebx, inp
                push eax
                CALL WriteConsoleA        


                mov eax,0
                push eax
                CALL ExitProcess
     main ENDP
     END main

For some reason, when i run the program, it first asks me for a string, then inputs the string and then exits. The second WriteConsoleA is not executed I guess. I was wondering if i needed to pop the registers. Help me out guys!
Thanks!
Devjeet

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by GunnerInc
0

Your first post, new to assembly? If so, why don't you use invoke instead of push/call? It will save you many headaches... You don't gain anything using push/calls.

You do not need lines 12, 13, and 14 they are already defined in kernel32.inc
that being said, all you have to do is push STD_OUTPUT_HANDLE not mov to eax then push

push    STD_OUTPUT_HANDLE
    call    GetStdHandle

that is the same as:

invoke  GetStdHandle, STD_OUTPUT_HANDLE

MASM32 has many functions and macros that help coding... I don't write console apps, but I whipped this up... should get you started

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
.data
prompt BYTE "Please Enter Your name: ", 0
prompt1 BYTE "Encrypted Name is: ",0 
szNewLine       BYTE    13, 10, 0
 
.data?
 inp BYTE 100 DUP(?)

.code

start:
   
    call main
    exit

main proc uses esi

    cls
    print   offset prompt
    invoke  memfill, offset inp, sizeof inp, NULL
    invoke  StdIn, offset inp, sizeof inp
    invoke  StripLF, offset inp
    invoke  szLen, offset inp
    
    mov     ecx, eax
    xor     esi, esi
    
L1:
    xor     inp[esi], 11101101b
    inc     esi
    loop    L1
        
    invoke  StdOut, offset prompt1
    invoke  StdOut, offset inp
    print   offset szNewLine
    
    inkey
    ret
main endp
end start
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.