-
Notifications
You must be signed in to change notification settings - Fork 6
/
printHexNumber.s
59 lines (42 loc) · 2.57 KB
/
printHexNumber.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# asmsyntax=as
# Makro pro pripravu a tisk hexadecimalni hodnoty na standardni vystup.
# - pro zapis je pouzita "Intel" syntaxe.
#
# Autor: Pavel Tisnovsky
.intel_syntax noprefix
# Makro pro vypis 32bitove hexadecimalni hodnoty na standardni vystup
# Jedinym parametrem makra je hodnota (konstanta)
.macro printHexNumber value
pusha # uschovat vsechny registry na zasobnik
mov edx, \value # hodnotu pro tisk ulozit do registru EDX
mov ebx, offset hexValueTemplate # adresu pro retezec ulozit do registru EBX
call hex2string # zavolani prislusne subrutiny pro prevod na string
writeMessage hexValueMessage, hexValueMessageLen # retezec je naplnen, tak ho muzeme vytisknout
popa # obnovit obsah vsech registru
.endm
#-----------------------------------------------------------------------------
.section .data
hexValueMessage:
.string "Hex value: 0x" # prvni cast zpravy
hexValueTemplate: # druha cast zpravy ma vlastni navesti
.string "????????\n" # otazniky budou prepsany
hexValueMessageLen = $ - hexValueMessage # delka zpravy
#-----------------------------------------------------------------------------
.section .text
# Subrutina urcena pro prevod 32bitove hexadecimalni hodnoty na retezec
# Vstup: EDX - hodnota, ktera se ma prevest na retezec
# EBX - adresa jiz drive alokovaneho retezce (resp. osmice bajtu)
hex2string:
mov cl, 8 # pocet opakovani smycky
print_one_digit: rol edx, 4 # rotace doleva znamena, ze se do spodnich 4 bitu nasune dalsi cifra
mov al, dl # nechceme porusit obsah vstupni hodnoty v EDX, proto pouzijeme AL
and al, 0x0f # maskovani, potrebujeme pracovat jen s jednou cifrou
cmp al, 10 # je cifra vetsi nebo rovna 10?
jl store_digit # neni, pouze prevest 0..9 na ASCII hodnotu '0'..'9'
alpha_digit: add al, 'A'-10-'0' # prevod hodnoty 10..15 na znaky 'A'..'F'
store_digit: add al, '0'
mov byte ptr [ebx], al # ulozeni cifry do retezce
inc ebx # dalsi ulozeni v retezci o znak dale
dec cl # snizeni pocitadla smycky
jnz print_one_digit # a opakovani smycky, dokud se nedosahlo nuly
ret # navrat ze subrutiny