x86 - Converting a hex character within Assembly -


i'm programming in assembly 8086.

the current task i'm doing converting 4 hex digits decimal representation. i've tried following tutors method i'm confused @ section have multiply hex digit temporary value, i've been told make 0 every time temp valued multiplied it's going 0 right?

the code have far below, appreciated!

; ------------------------------------ ; name    : gethex ; function: converts word (4 hex digits) numerical value ; inputs  : upto word hex values input console ; outputs : return within dx numerical conversion ; ------------------------------------  gethex: mov bx,0h ;temp value 0 mov cx,0h ;counter set 0 gethexloop: call getch push dx ; putch requires dl, need save current reg mov dl,al call putch pop dx ; restore dl reg mov bh,ah ;use bh temp storage ah corrupted cmp al,30h ; ascii 0 jl gethexloop ; if less 0 jump start not alphabetical char cmp al,39h ; ascii 9 jle nums ; if less or equal 9 number cmp al,46h jle case ; start conversion procedure case: sub al,10 jmp convert nums: sub al,30 ; convert normal number convert: push ax ; prepare multiply mov ax,bx mul bx pop ax add ax,bx add dx,ax ; final value add cx,1 ; increase counter cmp cx,3 ; if greater 4 (0 being included 3) jg endloop jmp gethexloop endloop: ret ; return calling statement 

just few observations... first, number number number. "hex" , "decimal" (and "binary" , "octal") ways represent number - it's same number. converting "hex representation" "decimal representation" in 1 step difficult (but possible... guess). want convert hex representation number... , convert number decimal representation can print it.

your "temp" variable, call "result far" set 0 begin with, not reset 0 each character/digit! general procedure set "result far" zero, each valid digit encountered, multiply "result far" radix - ten decimal, 16 hex - add in new digit... converted character number, of course. repeat until run out of characters

mul fine instruction, has "hidden" operands (ax , dx, in our case). multiplying ten, don't have choice, multiplying 16 can accomplished shl reg, 4 fewer "side effects". if you're on 8086, 1 , cl valid operands. "immediate" other 1 introduced in 80186 (80286 practical purposes - 80186 rare). can use it, perhaps you're not "supposed to". suspect you're "supposed to" use mul - makes easier check overflow, in case (if wish - limiting input 4 characters should avoid overflow anyway).

i don't see purpose of mov bh, ah line - ah contain "interesting" @ point?

almost assembler accept cmp al, '0' - same code cmp al, 48 or cmp al, 30h. find former make purpose of instruction clearer, doesn't matter.

jl , jg signed numbers - 0ffh interpreted -1, , compare "less than" zero. in code, won't encounter numbers "big enough negative" won't matter, jb , ja unsigned numbers (0ffh interpreted 255) , might "more correct". that's nit-pick, might learn difference - matter.

    cmp al,46h     jle case ; start conversion procedure case:     sub al,10 

again, cmp al, 'f' better - produces same code. if al not less or equal (again jbe correct unsigned), fall through case: anyway. doubt if that's want! , don't see why you're subtracting 10. have thought 37h, or 7 if you're going subtract '0' anyway.

the pesky user enter 'a'..'f' 'a'..'f'. proper "toupper", want make sure character has "case". here, we're going rejecting other 'a'..'f' anyway, won't matter if produce junk forcing characters don't have "uppercase". make sure you've disposed of decimal digits first! and al, 0dfh ; force uppercase. may or may not want - force user use shift key. :)

user input ends carriage return - 13 or 0dh - (as recall... linux uses linefeed - 10 or 0ah - think dos 13). might want check first - before decimal or hex digits. think current setup wait 4 characters user, if they've hit 'enter'. not expected behavior...

and yes, debugger , learn how use it. seeing what's happening step step lot. used david lindauer's grdb... (like debug brighter)...


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -