11) How To Cut A String

Copy a string to a destination with a max character limit.

Show Code

Oops! You must be logged in as a PRO MEMBER to see the code for this post. If you're a returning member, please click the LOGIN tab above to log in. If you're not a member yet, head over the Home page and scroll down to SIGN UP 🙂

12) How To Do A Bubble Sort

Make a bubble sorting algorithm.

Show Code

Oops! You must be logged in as a PRO MEMBER to see the code for this post. If you're a returning member, please click the LOGIN tab above to log in. If you're not a member yet, head over the Home page and scroll down to SIGN UP 🙂

PE1) First 20 Fibonacci

Write a program that uses a loop to calculate at least the first 20 values of the Fibonacci number sequence.

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
fibonacci DWORD 20 DUP(?)
result DWORD 0
.code
main PROC
	mov esi, OFFSET fibonacci            ;esi points to the beginning of fib array
	mov ecx, LENGTHOF fibonacci-1        ;Loop counter ecx = 6
	mov eax, 0
	mov ebx, 1

	mov [esi], eax                       ;1st element in Fibarray = 0
	add esi, TYPE fibonacci	             ;increment array

	mov [esi], ebx                       ;2nd element in Fibarray = 1
	add esi, TYPE fibonacci              ;increment array

L2:
	mov eax, [esi-8]                ;eax = value of the 2 elements before esi is pointing at
	mov ebx, [esi-4]                ;eax = value of the previous element before esi is pointing at
	mov edx, eax
	add edx, ebx                         ;edx = eax + ebx
	mov [esi], edx                       ;store value in edx in fib array
	add esi, TYPE fibonacci              ;increment esi
loop L2

INVOKE ExitProcess,0
main ENDP
END main

Show Code

PE2) Big Endian To Little Endian

Use MOV instructions to copy the value from bigEndian to littleEndian, reversing the order of the bytes.  The number’s 32-bit value is 12345678h.

; Program Name:  bigEndian to LittleEndian
; Description: 

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
bigEndian BYTE 12h,34h,56h,78h
littleEndian BYTE 0

.code
main PROC
	mov al,[bigEndian+3]
	mov littleEndian,al

	mov al,[bigEndian+2]
	mov littleEndian+1,al

	mov al,[bigEndian+1]
	mov littleEndian+2,al

	mov al,[bigEndian]
	mov littleEndian+3,al

INVOKE ExitProcess,0
main ENDP
END main

Show Code

PE3) Setting Flags

Set the zero flag, carry flag, and overflow flag.

; Program Name: setting flags
; Program Description:  

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
myByte BYTE 10001111b
.code
main PROC
mov eax, 0;
mov ebx, 0;

;1  Copy the value defined by the myByte data definition 
;   into the AX register such that the result is positive.
	movzx ax, myByte

;2  Copy the value defined by the myByte data definition 
;   into the BX register such that the result is negative.
	movsx bx, myByte
	inc bx                  ;makes sure the zero flag is 0 (ZR)
	

;3  Perform an arithmetic operation that causes the zero flag to be set.
;   make sure carry flag is zero(cy)
	sub al, myByte          ;set zero flag
	inc al

;4  Using the value defined by the myByte data definition, add 
;   instructions that cause the carry flag to be set.
	mov al, myByte
	add al, 256-10001111b   ;set carry
	mov al, 1               ;make sure the overflow flag is zero(ov)
	inc al

;5  Using the value defined by the myByte data definition, add 
;   instructions that cause the overflow flag to be set.
	mov al, myByte
	add al, myByte          ;set overflow

INVOKE ExitProcess,0
main ENDP
END main

Show Code

PE4) Add Two Arrays And Store The Result In A New Array

Write a program that adds each value in the valueB array to the corresponding value in the valueD array and stores the result in the result array.

valueB BYTE 1,2,3,-1,-2,-3
valueD DWORD 6,5,4,3,2,1
result DWORD LENGTHOF valueD dup(0)

; Program Name:  Add Two Arrays and Store the result in a result array
; Description: 

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
valueB BYTE 1,2,3,-1,-2,-3
valueD DWORD 6,5,4,3,2,1
result DWORD LENGTHOF valueD dup(0)

.code
main PROC
	mov eax, 0                               ;clarity purposes only
	mov ecx, LENGTHOF valueB                 ;loop counter: ecx = 6
	mov edi, 0
	mov esi, 0

L1:
	mov al, [valueB + esi * TYPE BYTE]       ;index scaling: al = first value in array
  movsx eax, al                            ;Must zero out the bytes and put it to larger register so we can add
	mov ebx,[valueD + esi * TYPE DWORD]      ;bx = first value in array
	mov result[edi], ebx					
	add result[edi], eax                     ;result = ebx+eax
	inc esi                                  ;inc by one because valueB is a BYTE.  
                                                 ;valueD is a dword but the *TypeDword 
                                                 ;takes care of that
	add edi, 4                               ;Add 4 because result is a DWORD
	loop L1

INVOKE ExitProcess,0
main ENDP
END main

Show Code

PE5) Array Exchange

Write a program with a loop with indexed addressing that exchanges every pair of values in an array with an even number of elements.  Therefore, item i will exchange with item i + 1, and item i + 2 will exchange with item i + 3, and so on.  Example:  [1,2,3,4,5,6,7,8]  →   [2,1,4,3,6,5,8,7]

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
array DWORD 1,2,3,4,5,6,7,8
.code
main proc
  ; must offset the array so that esi regester 
  ; holds the address of the first item in the array
	mov esi, OFFSET array			
  ; ecx register is now 4
	mov ecx, LENGTHOF array / 2				

myLoop: ; will loop 4 times because ecx has 4
  ; get the pair numbers from array
	mov eax, [esi]
	mov ebx, [esi + 4]

  ; swap the pairs in array
	mov [esi], ebx
	mov [esi + 4], eax

  ; A DWORD is 4 bytes so we must increment the 
  ; esi register by 8 to get the next pair.
	add esi, TYPE array * 2

loop myLoop

	invoke ExitProcess,0
main endp
end main

Show Code

PE6) Reverse A String Using Modules

  • Reverse the letters of The Alphabet.
  • Create two modules for your program.
  • Create an include file.
  • Pass the procedure the offsets for two-byte arrays, one being the original string and the other a buffer large enough to contain the reversed Alphabet.
  • Any additional info required to reverse the string should be passed.

INCLUDE PE6)IncludeFile.inc           ;needed to include

.data
gandhi	BYTE	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
nameSize = ($ - gandhi) - 1
buffer	BYTE	nameSize DUP(?)       ;buffer will contain the reversed string
.code
main PROC
	;pushing items on stack. :ESP is decremented
	push OFFSET gandhi + nameSize ;pushing gandhi (points at the end of gandhi array)
	push OFFSET buffer            ;pushing buffer (points to the front of buffer array)
	push LENGTHOF gandhi			

	call mainPortion              ;calling mainPortion procedure within MOD1
	
	INVOKE ExitProcess,0 ;exit
main ENDP

END main

Module 1:

; When making this program, call it PE6)Module1.asm

INCLUDE PE6)IncludeFile.inc         ;needed

.code                               ;needed
reverseString PROC
	;index adressing
L1:
	mov al, [esi]
	mov [edi], al
	dec esi
	inc edi
	loop L1
	ret
reverseString ENDP
END

Module 2:

; When making this program, call it PE6)Module2.asm

INCLUDE PE6)IncludeFile.inc         ;needed

.code                               ;needed
reverseString PROC
	;index adressing
L1:
	mov al, [esi]
	mov [edi], al
	dec esi
	inc edi
	loop L1
	ret
reverseString ENDP
END

Include File:

; When you make this file, call it PE6)IncludeFile.inc

.386                                   ;directive needed for programt
.model flat,stdcall                    ;directive needed for programt
.stack 4096                            ;directive needed for programt
ExitProcess PROTO, dwExitCode:DWORD

mainPortion PROTO               ;prototype to procedure in Module1 (Need this)
reverseString PROTO             ;prototype to procedure in Module2 (Need this)

Show Code

PE7) Calling Functions From C

Create some procedures that utilize the C Standard Library.

Driver:

Show Code

Oops! You must be logged in as a PRO MEMBER to see the code for this post. If you're a returning member, please click the LOGIN tab above to log in. If you're not a member yet, head over the Home page and scroll down to SIGN UP 🙂

PE8) Color Strings

Use a loop to display 50 lines of text, each with a randomly chosen color among three colors for each line. The probabilities for the colors are: 10% Dark Purple, 30% green, 60% grey.

Driver:

Show Code

Oops! You must be logged in as a PRO MEMBER to see the code for this post. If you're a returning member, please click the LOGIN tab above to log in. If you're not a member yet, head over the Home page and scroll down to SIGN UP 🙂