Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

lab manual and assignment

Example: Simple Procedure


TITLE SimpleProcedure
INCLUDE Irvine32.inc
.data 
a dword  6
b dword   8
.code
main PROC
mov eax, a
mov ebx, b 
call AddTwo 
call writeDec 
exit
main ENDP
average PROC
mov ebx,2
mov edx,0
div ebx 
ret 
average ENDP
AddTwo PROC
add eax,ebx 
call average
ret 
AddTwo ENDP
END main
END main

Example: Write String to Console using windows API


.NOLIST
.686P       
.MODEL flat, stdcall
.STACK 4096
HANDLE TEXTEQU <DWORD>      
WriteConsole    EQU <WriteConsoleA>
ReadConsole     EQU <ReadConsoleA>
STD_INPUT_HANDLE EQU -10
STD_OUTPUT_HANDLE EQU -11       
STD_ERROR_HANDLE EQU -12
exit EQU <INVOKE ExitProcess,0>     
ExitProcess PROTO,                 
    dwExitCode:DWORD        
GetStdHandle PROTO,                 
    nStdHandle:HANDLE       
WriteConsole PROTO,     
    hConsoleOutput:HANDLE,      
    lpBuffer:PTR BYTE,      
    nNumberOfCharsToWrite:DWORD,        
    lpNumberOfCharsWritten:PTR DWORD,       
    lpReserved:DWORD
ReadConsole PROTO,
    hConsoleInput:HANDLE,              
    lpBuffer:PTR BYTE,                  
    nNumberOfBytesToRead:DWORD,     
    lpNumberOfCharsRead:PTR DWORD,      
    lpReserved:DWORD                
GetConsoleMode PROTO,
    hConsoleHandle:HANDLE,      
    lpMode:PTR DWORD
SetConsoleMode PROTO,
    hConsoleHandle:HANDLE,      
    dwMode:DWORD
.LIST
.data
consoleInHandle  DWORD ?        
consoleOutHandle DWORD ?        
bytesWritten     DWORD ?
bytesRead        DWORD ?
InitFlag DB 0
msg byte "Hello  ",0
prompt byte "Enter Your Name",0Dh,0Ah,0
buffer BYTE 21 DUP(0)
;---------------------------------------------------------
.code
main proc
mov edx,offset prompt
call cout 
mov edx,OFFSET buffer 
mov ecx,SIZEOF buffer
call cin
mov edx, offset msg 
call cout
 mov edx,OFFSET buffer
call cout

exit
main endp
Str_length PROC USES edi,
    pString:PTR BYTE    
    mov edi,pString
    mov eax,0                      
L1:
    cmp BYTE PTR [edi],0          
    je  L2                       
    inc edi                      
    inc eax                      
    jmp L1
L2: ret
Str_length ENDP
Initialize PROC
    pushad
    INVOKE GetStdHandle, STD_INPUT_HANDLE
    mov [consoleInHandle],eax
    INVOKE GetStdHandle, STD_OUTPUT_HANDLE
    mov [consoleOutHandle],eax

    mov InitFlag,1
    popad
    ret
Initialize ENDP
CheckInit MACRO
LOCAL exit
    cmp InitFlag,0
    jne exit
    call Initialize
exit:
ENDM
cout PROC
    pushad
    CheckInit
    INVOKE Str_length,edx       
    cld 

    INVOKE WriteConsole,
        consoleOutHandle,
        edx,    
        eax,    
        OFFSET bytesWritten,    
        0
    popad
    ret
cout ENDP
;--------------------------------------------------------
cin PROC
    LOCAL bufSize:DWORD, saveFlags:DWORD, junk:DWORD
.data
_$$temp DWORD ?            
.code
    pushad
    CheckInit
    mov edi,edx            
    mov bufSize,ecx       
    push edx
    INVOKE ReadConsole,
      consoleInHandle,        
      edx,                  
      ecx,                  
      OFFSET bytesRead,
      0
    pop edx
    cmp bytesRead,0
    jz  L5                    

    dec bytesRead                  
    cld     
    mov ecx,bufSize           
    mov al,0Ah                 
    repne scasb
    jne L1                     
    dec bytesRead                  ; second adjustment to bytesRead
    sub edi,2                       ; 0Ah found: back up two positions
    cmp edi,edx                    ; don't back up to before the user's buf
fer
    jae L2
    mov edi,edx                    ; 0Ah must be the only byte in the buffe
r
    jmp L2                      ; and jump to L2

L1: mov edi,edx                ; point to last byte in buffer
    add edi,bufSize
    dec edi
    mov BYTE PTR [edi],0              ; insert null byte

    INVOKE GetConsoleMode,consoleInHandle,ADDR saveFlags
    INVOKE SetConsoleMode,consoleInHandle,0
L6: INVOKE ReadConsole,consoleInHandle,ADDR junk,1,ADDR _$$temp,0
    mov al,BYTE PTR junk
    cmp al,0Ah      ; the terminal line feed character
    jne L6          ; keep looking, it must be there somewhere
    INVOKE SetConsoleMode,consoleInHandle,saveFlags    ; restore console mod
e
    jmp L5
L2: mov BYTE PTR [edi],0        ; insert null byte at end of string
L5: popad
    mov eax,bytesRead
    ret
cin ENDP
END main

Example: Read String from Console using Windows API

Example: Encryption system


TITLE Encryption Program (Encrypt.asm)
INCLUDE Irvine32.inc
KEY = 239 ; any value between 1-255
BUFMAX = 128 ; maximum buffer size
.data
sPrompt BYTE "Enter the plain text:",0
sEncrypt BYTE "Cipher text: ",0
sDecrypt BYTE "Decrypted: ",0
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD ?
.code
main PROC
call InputTheString ; input the plain text
call TranslateBuffer ; encrypt the buffer
mov edx,OFFSET sEncrypt ; display encrypted message
call DisplayMessage
call TranslateBuffer  ; decrypt the buffer
mov edx,OFFSET sDecrypt ; display decrypted message
call DisplayMessage
exit
main ENDP
;-----------------------------------------------------
InputTheString PROC
;
; Prompts user for a plaintext string. Saves the string 
; and its length.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------
pushad ; save 32-bit registers
mov edx,OFFSET sPrompt ; display a prompt
call WriteString
mov ecx,BUFMAX ; maximum character count
mov edx,OFFSET buffer  ; point to the buffer
call ReadString  ; input the string
mov bufSize,eax  ; save the length
call Crlf
popad
ret
InputTheString ENDP
;-----------------------------------------------------
DisplayMessage PROC
;
; Displays the encrypted or decrypted message.
; Receives: EDX points to the message
; Returns: nothing
;-----------------------------------------------------
pushad
call WriteString
mov edx,OFFSET buffer ; display the buffer
call WriteString
call Crlf
call Crlf
popad
ret
DisplayMessage ENDP
;-----------------------------------------------------
TranslateBuffer PROC
;
; Translates the string by exclusive-ORing each
; byte with the encryption key byte.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------
pushad
mov ecx,bufSize ; loop counter
mov esi,0 ; index 0 in buffer
L1:
xor buffer[esi],KEY ; translate a byte
inc esi ; point to next byte
loop L1
popad
ret
TranslateBuffer ENDP
END main

Example: Recursive procedure

TITLE Calculating a Factorial (Fact.asm)
INCLUDE Irvine32.inc
.code
main PROC
push 5 ; calc 5!
call Factorial ; calculate factorial (EAX)
call WriteDec ; display it
call Crlf
exit
main ENDP
;----------------------------------------------------
Factorial PROC
; Calculates a factorial.
; Receives: [ebp+8] = n, the number to calculate
; Returns: eax = the factorial of n
;----------------------------------------------------
push ebp
mov ebp,esp
mov eax,[ebp+8] ; get n
cmp eax,0 ; n 0?
ja L1 ; yes: continue
mov eax,1 ; no: return 1 as the value of 0!
jmp L2 ; and return to the caller
L1: dec eax
push eax ; Factorial(n 1)
call Factorial
; Instructions from this point on execute when each 
; recursive call returns.
ReturnFact:
mov ebx,[ebp+8]  ; get n
mul ebx  ; EDX:EAX = EAX * EBX
L2: pop ebp ; return EAX
ret 4 ; clean up stack
Factorial ENDP
END main

Example: procedure with local variables


TITLE Swap Procedure Example (Swap.asm)
INCLUDE Irvine32.inc
Swap PROTO, pValX:PTR DWORD, pValY:PTR DWORD
.data
Array DWORD 10000h,20000h
.code
main PROC
; Display the array before the exchange:
mov esi,OFFSET Array
mov ecx,2 ; count = 2
mov ebx,TYPE Array
call DumpMem ; dump the array values
INVOKE Swap, ADDR Array, ADDR [Array+4]
; Display the array after the exchange:
call DumpMem
exit
main ENDP
;-------------------------------------------------------
Swap PROC USES eax esi edi,
pValX:PTR DWORD, ; pointer to first integer
pValY:PTR DWORD ; pointer to second integer
;
; Exchange the values of two 32-bit integers
; Returns: nothing
;-------------------------------------------------------
mov esi,pValX ; get pointers
mov edi,pValY
mov eax,[esi] ; get first integer
xchg eax,[edi] ; exchange with second
mov [esi],eax ; replace first integer
ret ; PROC generates RET 8 here
Swap ENDP
END main

You might also like