2

I'm using assembly 8086 16BIT with tasm assembler. I'm trying to print an int variable, and to do so I need to converet my variable contant to string. I tried to build a procedure that do this without success. its completely wrong and not working.

can you help me build this/explain how to build this?

Thanks guys!

This is my base code right now:

stepCounter     db  0   
push offset stepCounter ; Copy the OFFSET of "parameter" into the stack
call toString

proc    toStringPrint
    push bp
    mov bp, sp

    mov  ax, [bp + 4] ;number
    div 10; div number(in ax) by 10
    mov [bx], ah

    ;mov  dx, []
    ;mov  ah, 9h
    ;int  21h

    pop bp
    ret 4
endp    toString

EDIT

thanks! this is my code now: but its still not print nothing

proc    toStringPrint
    push bp
    mov bp, sp

    mov si, [bp+4];number
    mov ax, [si]
divide:
    cmp al, 0
    je Print
    mov cl, 10
    div cl; div number(in ax) by 10
    mov [bx], ah
    dec bx  
    jmp divide

Print:  
    mov  dx, [bp + 6]
    mov  ah, 9h
    int  21h

    pop bp
    ret 4
endp    toStringPrint

EDIT 2 This is the current code, still crash the application and always print 219:

stepCounter     dW  0

;this is how i call the PROC:
mov cx, [stepCounter]
push cx   
call toStringPrint

proc    toStringPrint
    push bp
    mov bp, sp

    mov si, [bp+4] ;number location in memory( I think )
    mov ax, [si]

    mov cl, "$"
    mov [bx], cl
divide:
    mov ah, 0
    mov cl, 10
    div cl         ; div number(in ax) by 10
    dec bx
    add ah, 48     ;Make into a character
    mov [bx], ah  
    cmp al, 0
    jne divide
Print:  
    mov dx, bx
    mov ah, 9h
    int 21h

    pop bp
    ret 4
endp    toStringPrint
Eden
  • 87
  • 4
  • 11

2 Answers2

2
mov  ax, [bp + 4] ;number

The comment on this line is wrong. At [bp+4] you'll find the address of the stepCounter not its value! Use something like:

mov si, [bp+4]
mov ax, [si]

Also make your stepCounter a word in stead of a byte.

stepCounter     dw  0

The div instruction can't use an immediate operand. Move the value to a register beforehand. Use CL since it seems you want to use BX for storing the result:

mov cl, 10
div cl

Your edit gets close to a solution! I don't see what you expect at [bp+6]. The first step is to close the forthcoming string with a $ sign and then start adding digits. To always show at least 1 digit do the test at the end. Never forget to zero the AH register before doing the division:

  mov cl, "$"
  mov [bx], cl
divide:
  mov ah, 0
  mov cl, 10
  div cl         ; div number(in ax) by 10
  dec bx
  add ah, 48     ;Make into a character
  mov [bx], ah  
  cmp al, 0
  jne divide
Print:  
  mov dx, bx
  mov ah, 9h
  int 21h
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
1

This answer just addresses your EDIT 2.

mov cx, [stepCounter]
push cx   
call toStringPrint

This code pushes the actual value of your stepCounter but in the procedure you treat it as the address of your stepCounter. Just start the toStringPrint proc with:

proc    toStringPrint
 push bp
 mov bp, sp
 mov ax, [bp+4] ;Value of stepCounter

 pop bp
 ret 4
endp    toStringPrint

This code returns and removes an extra 4 bytes from the stack but you only pushed 2 bytes on the stack! Change this to:

 pop bp
 ret 2
endp    toStringPrint

You've not shown this but do make sure that BX points at the last byte of a suitable buffer. A 4-byte buffer will suffice.

Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • Glad to see that you got it working! Please remember to _upvote_ answers that helped you. I would suggest though that you _accept_ the answer by @SepRoland since he did the most work. – Fifoernik Jun 11 '16 at 13:27
  • thanks again :) I up voted but it said : "Thanks for the feedback! Once you earn a total of 15 reputation, your votes will change the publicly displayed post score." :( – Eden Jun 12 '16 at 18:41