Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

bug in math routine

CPU MasterCPU Master Member Posts: 48
hi there fellow ASM freaks... I can't figure out why this routine
stores a 1 in AX instead of the correct value of 'mynum'
my routine worked until I decided to add error checking.
(NASM source code) It is supposed to take a word, but display only
a byte in ascii form on the screen. It should trap the error of
mynum being greater than 255.


bits 16
org 100h

xor ax, ax
mov ax, [word mynum]
push ax
call DispAscWord
mov ah, 4Ch
int 21h

DispAscWord:

jmp begin

nogood:
mov ah, 9
mov dx, err
int 21h
ret

err db 'Number >255 Illegal$'

begin:
mov bp, sp
mov ax, [bp+2]
cmp ax, 255
ja nogood

push ax
mov bx, 10
mov cx, 0
digit:
cmp al, 0
jz prt_back
div bl
mov dl, ah
push dx
and ah, 0
inc cx
mov dx, 0
jmp digit

prt_back:
mov ah, 2

next_digit:
pop dx
add dl, 30h
int 21h
loop next_digit
pop ax
ret


mynum dw 256

i know it's far from optimized - but if you know a better method feel free to slap me. ;)

Thanks!

Comments

  • CroWCroW Member Posts: 348
    for learning purposes its surely a good idea to do take function-arguments manually from stack to get a feeling for adressing,the stack and assembly in general.to improve performance (at cost of compatibility to highlevel-languages) you may want to pass arguments through registers instead with stack.
    however,when writing larger programs its a good idea to let the assembler/compiler take care for argument-passing.

    example for masm/tasm:
    [code]
    .MODEL SMALL,PASCAL ; setting calling-convention
    .DATA
    MyNum dw 123

    .CODE
    Main PROC
    invoke DispAscii,[MyNum]
    mov ah,4ch
    int 21h
    Main ENDP

    DispAscii PROC wNum:WORD
    ....
    ret
    DispAscii ENDP
    END Main
    [/code]


Sign In or Register to comment.