【问题标题】:Assembly: How to print offset values装配:如何打印偏移值
【发布时间】:2012-02-24 14:53:32
【问题描述】:

我正在尝试打印偏移值。 (“打印”这个词对吗?还有别的词吗?

代码正确吗?我对组装文档感到很困惑。

print_offsets:  mov  SI,0
                mov  CX,30
                mov  AH,2
                int  21h
                jmp  offsloop

offsloop:       cmp  0,Array[SI]
                ja   print_offset ;if the array element is nonzero
                inc  SI
                dec  CX
                jnz  offsloop

print_offset:   mov DL,SI
                mov  AH,2
                int  21h

【问题讨论】:

    标签: string assembly printing int offset


    【解决方案1】:

    如果您尝试打印数字,则您的代码不正确。

    INT 21h, AH=2 输出 ASCII character。您的代码正在做的是将偏移值放入 DL。 DOS 会将该偏移值视为 ASCII 字符并输出。

    例如,假设第一个非零元素的偏移量为 7。您的代码将调用 INT 21h, AH=2DL=07。 DOS 将输出 ASCII 字符 07h,即 BEL(基本上是系统哔声)。相反,您可能需要DL=37h 来输出代表数字 7 位的 ASCII 字符 37h。

    有几种方法可以解决这个问题。

    第一种方法很简单——如果你的数组从不超过 10 个元素,你可以简单地将 30h 添加到偏移量中,将偏移量值转换为正确的 ASCII 字符值:

    print_offsets:  mov  SI,0          ; SI=offset
                    mov  CX,10         ; CX = count (must be <= 10!!!!)
    
    offsloop:       cmp  0,Array[SI]
                    ja   print_offset ;if the array element is nonzero
    next_element:   inc  SI
                    dec  CX
                    jnz  offsloop
                    jmp finished
    
    print_offset:   mov DL,SI
                    add DL, 30h      ; convert offset to ASCII digit 0..9
                    mov  AH,2
                    ; save the registers in case INT 21h modifies them!
                    push cx          ; save current count
                    push si          ; save current offset
                    int  21h
                    pop si           ; restore current offset
                    pop cx           ; restore current count
                    jmp next_element
    finished:
                    ; do something else!
    

    第二种方式更复杂,因为您需要实现完整的整数到ASCII 转换例程。但是有很多代码示例可以做到这一点。

    【讨论】:

    • 为什么需要保存当前计数?
    • 执行将继续到 print_offset 之后的下一个数组元素。如果 INT 21 修改了 cx 寄存器,计数值(您仍在使用)将是错误的。
    • 我明白了。那么 cx 和 si 被推到了哪里呢?自己的 cx[] 和 si[] 数组?
    • 没有。 PUSH 和 POP 是通用 x86 指令,用于将数据放入(和取出)堆栈。堆栈是 SP 寄存器指向的内存区域。您可能需要阅读有关基本 x86 架构的更多信息才能真正了解这里发生了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2023-03-14
    • 1970-01-01
    • 2011-07-02
    • 2013-07-07
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多