DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Assembly (http://www.daniweb.com/forums/forum125.html)
-   -   Nasm to tasm code help (http://www.daniweb.com/forums/thread41360.html)

deutsch Mar 18th, 2006 10:38 am
Nasm to tasm code help
 
Is there someone who could help me convert this
to Tasm style.

Thanks.
; celsius.asm Nasm code
;                      Need this converted to Tasm style
;                      so I can help him

BITS 16        ;16 bit instructions used
    ORG  100h          ;start of .com program


    SECTION .text    ;text section follows


main:
        mov    si,prompt1
        call    PrintString

        mov    di,Instring
        call    ReadString
        call    CRLF                ;INPUT Instring$
        mov    si,Instring
        call    StrToNum            ;ax = VAL(InString$)

; -------  ax = (int)((ax - 32) * 5 / 9) -----------

        sub    ax,32    ;ax = ax - 32
        mov    bx,5
        imul    bx      ;ax = ax * 5
        mov    bx,9
        idiv    bx      ;ax = ax / 9 (dx = remainder, ax = quotient)
        xchg    ax,dx    ;now ax = remainder and dx = quotient

; round up the value
        mov    bl,5
        idiv    bl      ;al = al / bl
        cbw              ;convert byte to word
        add    ax,dx    ;ax = ax + quotient

        mov    si,Instring
        call    NumToStr            ;InString$ = STRING$(ax)
        mov    si,Instring
        call    PrintString
        call    CRLF                ;PRINT InString$

;--------------------------------------------------
        mov    ax,4C00h
        int    21h

;** READ KEYBOARD TO STRING POINTED TO BY DI **
;** ReadString(SI) **

ReadString:
        push    dx
        push    ax
        push    di
        push    bx
        mov    bx,di
NextChar:
        sub    ah,ah
        int    16h    ;wait for key function
        cmp    al,13  ;[ENTER] key?
        jz      Done
        cmp    al,8    ;[<-] key?
        jz      BackSpace
        mov    [di],al ;save character
        inc    di
        mov    dl,al  ;
        mov    ah,2
        int    21h    ;echo character
        jmp    NextChar

BackSpace:
        cmp    di,bx  ;at beginning?
        jz      NextChar        ;yep no back space
        dec    di
        mov    ah,2
        mov    dl,8
        int    21h    ;backspace
        mov    ah,2
        mov    dl,' ';
        int    21h    ;PRINT " ";
        mov    ah,2
        mov    dl,8
        int    21h    ;backspace
        jmp    NextChar

Done:
        sub    al,al  ;clear al
        mov    [di],al ;null string
        pop    bx
        pop    di
        pop    ax
        pop    dx
        ret

;** PRINT STRING POINTED TO BY SI **
PrintString:
        push    dx
        push    ax
        push    si
PrintLoop:
        mov    al,[si]
        inc    si
        or      al,al  ;test for al=0
        jz      Finish
        mov    dl,al
        mov    ah,2
        int    21h    ;print character
        jmp    PrintLoop
Finish:
        pop    si
        pop    ax
        pop    dx
        ret

CRLF:
        push    ax
        push    dx
        mov    ah,2
        mov    dl,13
        int    21h
        mov    dl,10  ;lf
        int    21h
        pop    dx
        pop    ax
        ret

;** Convert string pointed to by SI to number in ax **
StrToNum:
        push    bx
        push    cx
        push    dx
        push    si
        mov    BYTE [sign],0    ;assume positive number
        sub    cx,cx  ;cx = 0
        mov    bx,10  ;base 10
        mov    al,[si]
        cmp    al,'-'
        jne    NextDigit ;positive number
        inc    si        ;skip '-' character
        mov    BYTE [sign],1      ;negative number
NextDigit:
        mov    al,[si] ;get digit
        inc    si
        sub    ah,ah  ;ah = 0
        sub    al,'0'  ;convert to 0-9 value
        jc      NotDecDigit
        cmp    al,10  ;
        ja      NotDecDigit
        xchg    ax,cx  ;ax = old value cx = new value
        mul    bx      ;ax = ax * 10
        add    cx,ax  ;add new result
        jno    NextDigit
        call    OverFlow
NotDecDigit:
        mov    ax,cx  ;ax = result
        cmp    dl,BYTE [sign]
        jz      positive
        neg    ax      ;was negative signed
positive:
        pop    si
        pop    dx
        pop    cx
        pop    bx
        ret

OverFlow:
        push    ax
        push    bx
        push    cx
        push    dx
        push    si
        call    CRLF
        mov    si,error2
        call    PrintString
        call    CRLF
        pop    si
        pop    dx
        pop    cx
        pop    bx
        pop    ax
        ret

;** Convert number in ax to string pointed to by DI **
NumToStr:
        push    ax
        push    dx
        push    cx
        push    bx
        push    di
        mov    bx,10
        xor    cx,cx
        cmp    ax,0
        jge    NotZero ;ax positive number
        neg    ax      ;2's complement
        mov    BYTE [di],'-'
        inc    di
NotZero:
        xor    dx,dx  ;set upper word of N to 0
        div    bx      ;N/10 and n mod 10
        add    dl,'0'  ;remainder in dl convert to asc
        push    dx      ;push one digit onto stack
        inc    cx      ;count digits
        or      ax,ax  ;n = 0?
        jne    NotZero
PopDigit:
        pop    ax      ;last digit first
        mov    [di],al ;
        inc    di
        loop    PopDigit
        sub    al,al  ;al = 0
        mov    [di],al ;null string
        pop    di
        pop    bx
        pop    cx
        pop    dx
        pop    ax
        ret

WaitKey:
        mov    ah,7
        int    21h
        ret

section .data


prompt1 db      '  Enter number: ',0

error1  db      'overflow',0
error2  db      'input OVERFLOW',0

sign    db      0      ;sign of number

section .bss

Instring        resb      256
WebN

Narue Mar 18th, 2006 10:57 am
Re: Nasm to tasm code help
 
>Is there someone who could help me convert this to Tasm style.
I'm curious why you asked us instead of the original author on alt.lang.asm. But if you insist on doing things the hard way, your best option is to learn enough TASM to notice the differences and the similarities. Then you can make the conversion yourself. Presumably you already have TASM, so you're in a good position to experiment with the translation until it works for you.

Might I suggest googling for "NASM TASM differences"?


All times are GMT -4. The time now is 4:10 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC