簡體   English   中英

比較FASM中的兩個字符串

[英]Compare two strings in FASM

關於FASM,我是新手,而對於ASM來說,我還是一個新手,但是我想比較存儲在“變量”中的兩個字符串: user_inputexit_cmd

目前,它可以正常組裝,但是當我在提示符下輸入任何內容時,它就會崩潰。 是的,我的代碼很亂,我要完成的任務似乎無法達到我所知道的ASM的水平,但是我已經用其他語言完成過,所以我正在ASM中進行嘗試。

您可以看到我使用宏CompareStrings (來源不明;不是我的)將字符串匹配的EAX設置為1,但是當我使用CMPEAX與1進行比較,然后將JE與標簽進行比較時,它拒絕工作。 有什么幫助嗎?

這是越野車代碼:

format PE console
;win32a.inc, win32w.inc, win64a.inc and win64w.inc
include 'win32w.inc'
entry main

; Define macros for things like printing strings ('print') and pausing (well, waiting for the user to press enter ('pause'))
macro print str {
      push str
      call [printf]
      push 0
}
macro pause {
      invoke system,'pause>nul'
}
macro getinput prompt {
      print prompt
      invoke gets,user_input
}
macro CompareStrings str1, str2
{
      lea edi, [str2]               ; edi -> address of string2
      dec edi                       ; edi = edi - 1
      .lab1:                        ; loop through all chars and compare each of them
      inc edi                       ; ds:di --> next character in string2
      lodsb                         ; al = next char from string1. loadsb increments si automatically
      cmp [edi], al                 ; compare characters
      jne .notfound                 ; jump out of the loop if they're unequal
      cmp al, 0                     ; chars are equal, but make sure we compared the entire string
      jne .lab1                     ; if not, continue the loop
      mov eax, 1                    ; else: strings are equal -> eax = 1
      ret                           ; return; result: strings are equal
      .notfound:                    ; chars are not equal
      mov eax, 0                    ; unequal -> eax = 0
      ret                           ; return; result: strings are not equal
}

section '.text' code readable executable

main:
        print header_msg
        jmp input_loop

input_loop:
        getinput cmd_prompt
        print newline
        CompareStrings user_input,cmd_exit
        cmp eax, 1
        je exit_cmd
        jne input_loop

exit_cmd:
        print header_msg
        ret

section '.data' data readable writable
        header_msg    db      "Basic Programming Language in ASM | Version 0.0.1",13,10,0
        cmd_prompt    db      "> ",0
        newline       db      "",13,10,0
        user_input    db      "",0dh,0ah,0
        chrarr        db      '%s',0dh,0ah,0
        cmd_exit      db      'exit',0

section '.idata' import data readable

library msvcrt,"msvcrt.dll"

import msvcrt,\
       puts,"puts",\
       getchar,"getchar",\
       exit,"exit",\
       printf,"printf",\
       scanf,"scanf",\
       system,"system",\
       gets,"gets"

這個程序有很多問題,它崩潰並不奇怪。

問題1

您沒有為輸入的字符串分配足夠的空間。 使用rb指令分配足夠大的空間來處理您的輸入:

user_input  rb  1024

將其放在數據部分的末尾,以免在結果可執行文件中發生。

問題2

我不知道如何使用CPP庫,但是可能調用約定不是STDCALL,參數也不完全是這些。 例如gets可能需要某種CPP自擴展字符串,而不是簡單的緩沖區地址。 否則,很容易使緩沖區溢出,並且無法檢查此類問題。

因此,最好使用普通的WinAPI-ReadFile和WriteFile來讀取和寫入控制台。

問題3

在上述背景下,這很挑剔,但無論如何:

  push str
  call [printf]
  push 0

push 0在這里看起來很奇怪。 應該怎么辦?

問題4

你的風格。 嘗試不使用“ {”和“}”字符來實現此程序。 這將幫助您更好地理解匯編語言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM