dollar-terminated strings

血红的双手。 提交于 2019-12-19 09:47:58

问题


In my assembly language class, our first assignment was to write a program to print out a simple dollar-terminated string in DOS. It looked something like this:

BITS 32
    global _main

section .data
    msg db "Hello, world!", 13, 10, ’$’

section .text
_main:
mov ah, 9
mov edx, msg
int 21h
ret

As I understand it, the $ sign serves to terminate the sting like null does in C. But what do I do if I want to put a dollar sign in the string (like I want to print out "it costs $30")? This seems like a simple question, but my professor didn't know the answer and I don't seem to find it using a google search.


回答1:


You can't use DOS's 0x09 service to display $ signs, you'll need to use 0x02. See here.




回答2:


Or make your own print_string to print a NULL-terminated string using the undocumented INT 29h (print character in AL).

; ds:si = address of string to print
print_string:
    lodsb                   ; load next character from ds:si
    or al, al               ; test for NULL-character
    jz .end_of_string       ; end of string encountered, return.
    int 29h                 ; print character in AL on screen
    jmp print_string        ; print next character
.end_of_string:
    ret                     ; return to callers cs:ip

(Assuming you are using NASM)




回答3:


Um. You could write assembly that would taken into account for escaped $, e.g. \$? But then your \ becomes a special symbol too, and you need to use \\ to print a \




回答4:


One way is to find the call that prints a single character. You can print any character with that. Break the string up and print "it costs ", then the '$', and finally, "30". More work, but it gets the job done.




回答5:


You can use 02 service of INT 21H instead of 09 service.

Here is the sample.

mov dl, '$'

mov ah,02

int 21h



回答6:


Try '$$', '\044' (octal) or '\x24' (hex)



来源:https://stackoverflow.com/questions/481344/dollar-terminated-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!