Instructions
Objective
Write a flat assembler program to convert a 8-bit integer to binary and hexadecimal using bitwise operators in flat assembler (fasm).
Requirements and Specifications
Do not use external functions. You may use the algorithm below.
1/8
Ask user to enter a number 0 - 255 and store into Num Algorithm to convert Value to binary:
- Set Count to 0. Move Num into a variable named Temp
- Move Temp into EAX then AND EAX with 128 (binary 10000000)
- if result is zero, output "O"
- if result is not zero, output "1"
- shift Temp left one digit
- increment Count
- if Count < 8, jump to step (2)
Algorithm to convert Value to hexadecimal:
- Move Num into a variable named Temp
- shift Temp right 4 digits to isolate left 4 digits
- if Temp <=9, print Temp
- if Temp >= 10, add 55 to Temp and print the ASCII character
- Move Num into a variable named Temp
- AND Temp with 240 (binary 00001111) to isolate right 4 digits
- if Temp <=9, print Value
- if Temp >=10, add 55 to Temp and print the ASCII character
Screenshots of output
Source Code
Balanced.asm
format PE console
include 'win32ax.inc'
;=======================================
section '.text' code readable executable
;=======================================
start:
cinvoke printf, "Enter an equation of ([{}]): "
cinvoke scanf, "%s", Equation
mov [InitialESP], ESP ; save initial stack value
mov EBX, -1 ;offset for input string
mainLoop:
inc EBX
mov EAX, 0 ; zero out EAX
mov AL, [Equation+EBX]
cmp AL, ' ' ; input token is a space - finish with mainloop
je finishedMainLoop
openParenthesis:
cmp AL, '('
jne openBracket
push EAX ; push ( onto stack
jmp mainLoop
openBracket:
cmp AL, '['
jne openBrace
push EAX ; push [ onto stack
jmp mainLoop
openBrace:
cmp AL, '{'
jne closeParenthesis
push EAX ; push { onto stack
jmp mainLoop
closeParenthesis:
cmp AL, ')'
jne closeBracket
cmp ESP, [InitialESP]
je Unbalanced
pop ECX
cmp ECX, '('
jne Unbalanced
jmp mainLoop
closeBracket:
cmp AL, ']'
jne closeBrace
cmp ESP, [InitialESP]
je Unbalanced
pop ECX
cmp ECX, '['
jne Unbalanced
jmp mainLoop
closeBrace:
cmp AL, '}'
jne mainLoop ; ignore other chars
cmp ESP, [InitialESP]
je Unbalanced
pop ECX
cmp ECX, '{'
jne Unbalanced
jmp mainLoop
finishedMainLoop:
cmp ESP, [InitialESP]
je Balanced
Unbalanced:
invoke printf, "Your equation is NOT balanced%c", 10
jmp ending
Balanced:
invoke printf, "Your equation IS balanced%c", 10
jmp ending
ending:
invoke Sleep,-1
ret
;======================================
section '.data' data readable writeable
;======================================
InitialESP: dd 0
Equation: times 100 db 32
;======================================
section '.idata' import data readable
;======================================
library kernel32, 'kernel32.dll', \
msvcrt,'msvcrt.dll'
import kernel32, \
ExitProcess,'ExitProcess', Sleep, 'Sleep'
import msvcrt, \
printf, 'printf', scanf, 'scanf'
Convert.asm
format PE console
include 'win32ax.inc'
;=======================================
section '.text' code readable executable
;=======================================
start:
cinvoke printf, "%cEnter an integer from 0-255: ", 10
cinvoke scanf, "%d", Num
call BeginBinary
call BeginHex
jmp start
BeginBinary:
cinvoke printf, "Binary: ", 10
mov DWORD[Count], 0
mov EAX, [Num]
mov [Temp], EAX
BinLoop:
mov EAX, [Temp]
and EAX, 128
jz Print0
cinvoke printf, "1"
jmp Continue
Print0:
cinvoke printf, "0"
Continue:
shl DWORD[Temp], 1
inc DWORD[Count]
cmp DWORD[Count], 8
jl BinLoop
ret
BeginHex:
cinvoke printf, "%cHex: ", 10
mov EAX, [Num]
mov [Temp], EAX
shr DWORD[Temp], 4
call PrintHexDigit
mov EAX, [Num]
mov [Temp], EAX
and DWORD[Temp], 15
call PrintHexDigit
ret
PrintHexDigit:
cmp DWORD[Temp], 9
jg GreaterThan9
cinvoke printf, "%d", DWORD[Temp]
ret
GreaterThan9: ; print A-F
add DWORD[Temp], 55
cinvoke printf, "%c", DWORD[Temp]
ret
;======================================
section '.data' data readable writeable
;======================================
Num: dd 0
Temp: dd 0
Count: dd 0
;======================================
section '.idata' import data readable
;======================================
library kernel32, 'kernel32.dll', \
msvcrt,'msvcrt.dll'
import kernel32, \
ExitProcess,'ExitProcess'
import msvcrt, \
printf, 'printf', scanf, 'scanf'
Random.asm
format PE console
include 'win32ax.inc'
;=======================================
section '.text' code readable executable
;=======================================
start:
cinvoke time, 0
cinvoke srand, EAX ; seed RNG
getRandomNumberA:
call getRandomNumber
mov [A], EDX
getRandomNumberB:
call getRandomNumber
cmp [A], EDX ; RN is in EDX
je getRandomNumberB
mov [B], EDX
getRandomNumberC:
call getRandomNumber
cmp [A], EDX ; RN is in EDX
je getRandomNumberC
cmp [B], EDX ; RN is in EDX
je getRandomNumberC
mov [C], EDX
getRandomNumberD:
call getRandomNumber
cmp [A], EDX ; RN is in EDX
je getRandomNumberD
cmp [B], EDX ; RN is in EDX
je getRandomNumberD
cmp [C], EDX ; RN is in EDX
je getRandomNumberD
mov [D], EDX
cinvoke printf, "Your distinct random numbers: %d %d %d %d %c", DWORD[A], DWORD[B], DWORD[C], DWORD[D], 10
getRandomNumberR:
call getRandomNumber
mov [R], EDX
dec DWORD[R] ; decrement to get 0-9
shr DWORD[R], 1 ; divide by 2 to get 0-4
cmp DWORD[R], 0
je phrase1
cmp DWORD[R], 1
je phrase2
cmp DWORD[R], 2
je phrase3
cmp DWORD[R], 3
je phrase4
jmp phrase5
phrase1:
cinvoke printf, "May the fourth be with you!"
jmp ending
phrase2:
cinvoke printf, "Never stop looking up!"
jmp ending
phrase3:
cinvoke printf, "Love, not hate"
jmp ending
phrase4:
cinvoke printf, "Believe you can and you are halfway there!"
jmp ending
phrase5:
cinvoke printf, "Be the change that you wish to see in the world"
jmp ending
getRandomNumber:
cinvoke rand
cdq
mov EBX, 10
idiv EBX
inc EDX
ret ; random number is in EDX
ending:
invoke Sleep, -1
;======================================
section '.data' data readable writeable
;======================================
R: dd 0
A: dd 0 ; RandomNum 1-10
B: dd 0 ; RandomNum 1-10
C: dd 0 ; RandomNum 1-10
D: dd 0 ; RandomNum 1-10
;======================================
section '.idata' import data readable
;======================================
library kernel32, 'kernel32.dll', \
msvcrt,'msvcrt.dll'
import kernel32, \
ExitProcess,'ExitProcess', Sleep, 'Sleep'
import msvcrt, \
printf, 'printf', scanf, 'scanf', \
time, 'time', srand, 'srand', \
rand, 'rand'