Howdy, Stranger!

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


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


jmp begin

mov ah, 9
mov dx, err
int 21h

err db 'Number >255 Illegal$'

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

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

mov ah, 2

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

mynum dw 256

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



  • 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 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:
    .MODEL SMALL,PASCAL ; setting calling-convention
    MyNum dw 123

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

    DispAscii PROC wNum:WORD
    DispAscii ENDP
    END Main

Sign In or Register to comment.