Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,722 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,906 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



GetHex Program in Assmebly using MASM

 
Reply to this topicStart new topic

GetHex Program in Assmebly using MASM

dr3am
post 12 May, 2008 - 02:54 PM
Post #1


New D.I.C Head

*
Joined: 11 May, 2008
Posts: 2

I have to write a function GetHex which has to read and echo hex digits from the keyborad and converts it into a word, which than returns its value in ax. I have to test for 4 hex digits. I have to use shift function somehow blink.gif I am not to sure about using the shift function

If someone can help me. it would be great.!!!!!!

CODE
INCLUDE F:\cs17\PCMAC.INC

        .MODEL  SMALL
        .586

        .STACK  100h

        .DATA
Count    DB    4

        .CODE
    PUBLIC    GetDec

GetDec    PROC

       ; ax not saved since it stores the return value
    push    bx
    push    cx
    push    dx



ReadLoop:
        _GetCh

        cmp     al, '0'        ; Is character a digit?
        jb      Done

        cmp     al, '9'
        ja      Done

        mov     ah, 0
        sub     al, '0'        ; '0' = 48 (al = al - 48)

        xchg    ax, bx         ; Add number to total
        mul     cx
        add     bx, ax
        
        
    dec    Count
        jmp     ReadLoop




Positive:
        mov     ax, bx         ; Returned value in ax

    pop    dx
    pop    cx
    pop    bx

        ret
GetDec    ENDP

        END
User is offlineProfile CardPM

Go to the top of the page


curvekiller
post 14 May, 2008 - 08:41 PM
Post #2


New D.I.C Head

*
Joined: 14 May, 2008
Posts: 1

You can convert a decimal to hexadecimal using shift left (note this operation takes place in binary).
Just like in a base 10 number system if you take 1 and add a zero to the left - 10 - it becomes 10 add another becomes 100 etc. Since 16 is 2^4 this is easy to do in binary just shift over 4 places and 15 becomes 240

An instruction like shl ax, 4 would move the value in the ax register to the left and add 0000,
making F = 15 = 00001111 into 11110000 = 240 = F0.

CODE

ReadLoop:
        _GetCh
  
    cmp    al, '0'        ; test if number is a digit
    jb    Done
    cmp    al, '9'
    jbe    Digi
    cmp    al, 'A'        ; test if number is a valid hex digit
    jb    Done
    cmp    al, 'F'    
    jbe    UHex
    cmp    al, 'a'        ; test if number is a valid hex digit
    jb    Done
    cmp    al, 'f'
    jbe    LHex
    cmp    al, 13    ; test if user hit enter key
    je     Done
    jmp    Done          ; if invalid character stop processing
    
Digi:
    mov     ah, 0
        sub     al, 30h        ; 30h = 48 decimal
    jmp    Conv    
    
LHex:
    and    al, 11011111B        ; Converts al to upper case
UHex:    
    mov     ah, 0
        sub     al, 37h          ; 37h = 87 decimal = [A-F] + 10

Conv:    
        xchg    ax, bx
        shl     ax, 4
        add     bx, ax; Add number to total
    jmp    ReadLoop

Done:    
        mov    ax, bx


This post has been edited by curvekiller: 14 May, 2008 - 08:56 PM
User is offlineProfile CardPM

Go to the top of the page

dr3am
post 16 May, 2008 - 04:21 PM
Post #3


New D.I.C Head

*
Joined: 11 May, 2008
Posts: 2

Thanks so much for the help. I would have never figured all of that out
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/8/08 09:42AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month