将整数转换为字符串程序集 8086 tasm [英] Convert integer to string assembly 8086 tasm

查看:26
本文介绍了将整数转换为字符串程序集 8086 tasm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 tasm 汇编器的汇编 8086 16BIT.我正在尝试打印一个 int 变量,为此我需要将我的变量 contant 转换为字符串.我试图建立一个程序,但没有成功.它完全错误并且不起作用.

你能帮我构建这个/解释如何构建这个吗?

谢谢各位!

这是我现在的基本代码:

stepCounter db 0推偏移 stepCounter ;将参数"的OFFSET复制到堆栈中调用字符串过程到字符串打印推bpmov bp, spmov ax, [bp + 4] ;数字div 10;div 数(在 ax 中)乘以 10mov [bx],啊;mov dx, [];移动啊,9h;int 21h流行音乐回复 4结束到字符串

编辑

谢谢!这是我现在的代码:但它仍然不打印任何内容

proc toStringPrint推bpmov bp, spmov si, [bp+4];数字mov ax, [si]划分:cmp al, 0je 打印mov cl, 10div cl;div 数(在 ax 中)乘以 10mov [bx],啊十二月jmp 除法打印:mov dx, [bp + 6]动啊,9h21 小时流行音乐回复 4endp toStringPrint

编辑 2这是当前代码,仍然使应用程序崩溃并始终打印 219:

stepCounter dW 0;这就是我调用 PROC 的方式:mov cx, [stepCounter]推cx调用字符串打印过程到字符串打印推bpmov bp, spmov si, [bp+4] ; 内存中的数字位置(我认为)mov ax, [si]mov cl, "$"mov [bx], cl划分:移动啊,0mov cl, 10div cl ;div 数(在 ax 中)乘以 10十二月add ah, 48 ;做成一个字符mov [bx],啊cmp al, 0jne鸿沟打印:mov dx, bx动啊,9h21 小时流行音乐回复 4endp toStringPrint

解决方案

此答案仅针对您的 EDIT 2.

<块引用>

mov cx, [stepCounter]推cx调用字符串打印

此代码推送您的 stepCounter 的实际值,但在程序中您将其视为您的 stepCounter 的地址.只需使用以下命令启动 toStringPrint 过程:

proc toStringPrint推bpmov bp, spmov ax, [bp+4] ;stepCounter 的值

<小时><块引用>

 pop bp回复 4endp toStringPrint

此代码返回并从堆栈中删除了额外的 4 个字节,但您只在堆栈上压入了 2 个字节!将其更改为:

 pop bp回复 2endp toStringPrint

<小时>

您没有显示这一点,但请确保 BX 指向合适缓冲区的最后一个字节.一个 4 字节的缓冲区就足够了.

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

解决方案

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.

这篇关于将整数转换为字符串程序集 8086 tasm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆