So this is the question:
Using the flat assembler (FASM), write a program that converts an 8-bit integer to binary and hexadecimal using bitwise operators. Do not use external functions. You may use the algorithm below.
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 "0"
- 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 15 (binary 00001111) to isolate right 4 digits
- if Temp <=9, print Value
- if Temp >=10, add 55 to Temp and print the ASCII character
Example output:
This x86 assembly program converts an integer to binary and hex.
Enter an integer from 0 - 255: 73
Binary: 01001001
Hex: 49
This is how I attempted it:
format PE console
include 'win32ax.inc'
section '.code' code readable executable
start:
cinvoke printf, "Enter an integer from 0-255:"
cinvoke scanf, "%d", Num
call BinStep1
call HexStep1
cinvoke printf, "%c%c", 10,10
jmp start
BinStep1:
cinvoke printf, "%cBin:",10
mov [Count],0
mov eax, [Num]
mov [Temp], eax
BinStep2:
mov eax, [Temp]
and eax, 128 ; result is either 0 or 128
BinStep3:
jnz BinStep4
;output a zero
jmp BinStep5
BinStep4:
;output a one
BinStep5:
;shift temp left one digit
BinStep6:
;increment Count
BinStep7:
cmp[Count], 8
jl BinStep2
ret
HexStep1:
cinvoke printf, "%cHex: ",10
mov eax, [Num]
mov [Temp], eax
HexStep2:
shr [Temp], 4
call PrintHexDigit
HexStep5:
mov eax, [Num]
mov [Temp], eax
HexStep6:
;And Temp with 15 to isolate right 4 bits
call PrintHexDigit
ret
PrintHexDigit:
cmp [Temp],9
jp GreaterThan9
cinvoke printf, "%d", [Temp]
ret
GreaterThan9: ;print A-F
add [Temp], 55
cinvoke printf, "%c",[Temp]
ret
section '.data' data readable writeable
Num dd 0
Temp dd 0
Count dd 0
section '.idata' import data readable
library msvcrt,'msvcrt.dll',kerne132,'kerne132.dll'
import msvcrt, printf,'printf',scanf,'scanf'
But my results did not came out as expected. This is how the output came out:
output after I run the program
Can anyone help me out in fixing this program?
BinStep
I wonder, did you expect something to come out when you didn't even replace the comments by actual instructions?
And anyway, the BinStep loop is really an infinite loop, because not incrementing Count will bring about that
cmp [Count], 8jl BinStep2will always be true and therefore forever jumping to the top of the loop!HexStep
Again I see a comment that didn't get translated to assembly. Just as easy as the
shr [Temp], 4that you knew about, this needs to becomeand [Temp], 15.The PrintHexDigit subroutine has a typo. The
jp GreaterThan9instruction is testing the parity flag. What you need isjg GreaterThan9I fully understand that you want to follow the provided algorithms to the letter, but you could remove that tail-call from the HexStep and still comply: