MASM str 和 substr?

发布于 2024-09-26 12:26:52 字数 664 浏览 6 评论 0原文

我目前正在用 asm 编写一个 irc 机器人 我已经在 C++ 中做过一次,所以我知道如何解决我遇到的大多数问题,但我需要一个像 C++ 中那样的 substr()[*] 函数。我需要 substr 函数从 PING 请求接收服务器名称,以便我可以响应相应的 PONG 响应

,但我不知道如何在 MASM 中实现它,我听说过一种叫做宏组装的东西,似乎 substr 经常用于这些函数

有谁知道如何让我的 substr 函数工作

[*] string substr ( size_t pos = 0, size_t n = npos )

这就是我在中使用 substr() 函数的方式C++:

if(data.find("PING :") != std::string::npos){
string pong = "PONG :" + data.substr(  (data.find_last_of(":")+1), (data.find_last_of("\r")-1)  );
SCHiMBot.Pong(pong);   // Keep the connection alive!
}

其中 data 是保存服务器发送给我的所有信息的字符串,SCHiMBot 是我用来与服务器通信的类 这段代码是直接从我编码的机器人中编译出来的,所以它应该是完美的

I'm currently coding an irc bot in asm
I have already done this once in C++, so I know how to solve most problems I encounter, but I need a substr()[*] function like the one seen in C++. I need the substr function to receive the server name from a PING request so I can respond with the corresponding PONG response

But I don't know how to implent it in MASM, I heard of something called macroassembling, It seems substr is often used in those functions

Does anyone have any idea how I can get my substr function to work

[*] string substr ( size_t pos = 0, size_t n = npos )

This is how I use the substr() funcion in C++:

if(data.find("PING :") != std::string::npos){
string pong = "PONG :" + data.substr(  (data.find_last_of(":")+1), (data.find_last_of("\r")-1)  );
SCHiMBot.Pong(pong);   // Keep the connection alive!
}

Where data is a string holding all the information the server sends me, and SCHiMBot is a class I use to talk with the server
This code is c&p'ed directly out of a bot I coded, so it should be flawless

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-10-03 12:26:52

这实际上并不像最初看起来那么容易回答。问题非常简单:像 substr 这样的函数并不是真正孤立存在的——它是字符串库的一部分,为了使其有用,您至少需要概述一下如何库作为一个整体组合在一起,如何表示数据等。即,substr 创建一个字符串,但要做到这一点,您需要确定字符串是什么

为了避免这个问题,我将忽略您实际询问的内容,并给出一个更适合汇编语言的更简单的答案。您真正需要的是从一个数据缓冲区开始,在该缓冲区中找到几个“标记”,然后将这些标记之间的内容复制到另一个缓冲区中的指定位置。首先我们需要代码来执行“find_last”:

; expects: 
; ESI = address of buffer
; ECX = length of data in buffer
; AH =  character to find
; returns:
; ESI = position of item
;
find_last proc 
    mov al, [esi+ecx]
    cmp ah, al
    loopnz  find_last
    ret
find_last endp

现在要将子字符串复制到传输缓冲区,我们执行如下操作:

CR = 13

copy_substr proc
    mov esi, offset read_buffer
    mov ecx, bytes_read
    mov ah, CR
    call find_last   ; find the carriage-return
    mov edx, esi     ; save its position

    mov esi, offset read_buffer
    mov ecx, bytes_read
    mov ah, ':'
    call find_last   ; find the colon
    inc esi          ; point to character following colon
    sub edx, esi     ; get distance from colon+1 to CR
    mov ecx, edx   

    ; Now: ESI = address following ':'
    ;      ECX = distance to CR

    mov edi, (offset trans_buffer) + prefix_length
    rep movsb         ; copy the data
    ret
copy_substr endp

This really isn't nearly as easy to answer is it might initially seem. The problem is pretty simple: a function like substr doesn't really exist in isolation -- it's part of a string library, and to make it useful, you just about need to at least sketch out how the library as a whole fits together, how you represent your data, etc. I.e., substr creates a string, but to do so you need to decide what a string is.

To avoid that problem, I'm going to sort of ignore what you actually asked, and give a somewhat simpler answer that's more suited to assembly language. What you really need is to start with one buffer of data, find a couple of "markers" in that buffer, and copy what's in between those markers to a designated position in another buffer. First we need the code to do the "find_last":

; expects: 
; ESI = address of buffer
; ECX = length of data in buffer
; AH =  character to find
; returns:
; ESI = position of item
;
find_last proc 
    mov al, [esi+ecx]
    cmp ah, al
    loopnz  find_last
    ret
find_last endp

Now to copy the substring to the transmission buffer, we do something like this:

CR = 13

copy_substr proc
    mov esi, offset read_buffer
    mov ecx, bytes_read
    mov ah, CR
    call find_last   ; find the carriage-return
    mov edx, esi     ; save its position

    mov esi, offset read_buffer
    mov ecx, bytes_read
    mov ah, ':'
    call find_last   ; find the colon
    inc esi          ; point to character following colon
    sub edx, esi     ; get distance from colon+1 to CR
    mov ecx, edx   

    ; Now: ESI = address following ':'
    ;      ECX = distance to CR

    mov edi, (offset trans_buffer) + prefix_length
    rep movsb         ; copy the data
    ret
copy_substr endp
零時差 2024-10-03 12:26:52
data.substr(  (data.find_last_of(":")+1)

substr 的第一个参数是起始位置。如果它是传递字符串最后一个元素的值,则会抛出 out_of_range 异常。您应该验证这种情况没有发生。

if(data.find("PING :") != std::string::npos)
{
    size_t s1 = data.find_last_of(":");
    size_t s2 = data.find_last_of("\r");

    if (s1 != string::npos &&
        s2 != string::npos &&
        s1+1 < data.size())
    {
        string pong = "PONG :" + data.substr(s1+1, s2-1);
        SCHiMBot.Pong(pong);   // Keep the connection alive!
    }
}
data.substr(  (data.find_last_of(":")+1)

The first parameter of substr is the starting position. If it is a value passed the last element of the string, out_of_range exception will be thrown. You should verify that this is not happening.

if(data.find("PING :") != std::string::npos)
{
    size_t s1 = data.find_last_of(":");
    size_t s2 = data.find_last_of("\r");

    if (s1 != string::npos &&
        s2 != string::npos &&
        s1+1 < data.size())
    {
        string pong = "PONG :" + data.substr(s1+1, s2-1);
        SCHiMBot.Pong(pong);   // Keep the connection alive!
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文