【问题标题】:Moving string between variables在变量之间移动字符串
【发布时间】:2015-11-09 01:15:50
【问题描述】:

我正在使用我教科书中的 Irvine32 库编写一个 MASM 程序。我想排列我的打印列,但是我找不到任何关于如何用制表位终止字符串的信息,/t 用于 C/C++,所以我做了一个程序来确定数字有多少个小数位并相应地分配spacer 字符串变量。

我的问题是在尝试分配 spacer 一个新字符串时,我遇到了组装错误。我试过了

mov spacer, "New String",0

mov spacer, "New String"

以及将"New String" 分配给一个变量并将spacer 分配给该变量以及该变量OFFSET。我现在有它,其中变量 OFFSET 被移动到 edx 但我不能将 edx 移动到另一个变量中。

非常感谢您的帮助和任何信息,以帮助更好地理解为什么这在汇编或 MASM 中不起作用。谢谢

有问题的程序:

; Define the spacer based on the places in x
spacer_choice proc

    cmp x, 10
    JB SC1

    cmp x, 100
    JB SC2

    mov edx, OFFSET hundo_spacer
    JMP SC3

    SC1:
        mov edx, OFFSET one_spacer
        JMP SC3

    SC2:
        mov edx, OFFSET ten_spacer
        JMP SC3

    SC3:
        mov spacer, OFFSET edx
        RET
spacer_choice endp

输出:

1>------ Build started: Project: MASM4, Configuration: Debug Win32 ------
1>  Assembling composite.asm...
1>composite.asm(209): error A2032: invalid use of register
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\BuildCustomizations\masm.targets(50,5): error MSB3721: The command "ml.exe /c /nologo /Sg /Zi /Fo"Debug\composite.obj" /Fl"MASM4.lst" /I "c:\Irvine" /W3 /errorReport:prompt  /Tacomposite.asm" exited with code 1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

完整代码:

INCLUDE Irvine32.inc

.data
; ----- GLOBALS
n_comps DWORD ?


; ----- INTRO VALUES
; Greeting the User
title_prompt BYTE "Welcome to Prog. 4, my name is Zach", 10, 13, 0
name_prompt BYTE "What should I call you?: ", 0
username BYTE 21 DUP(0)
user_greet BYTE "Hello, ", 0
; Instructing the User
one_instruct BYTE "We are going to calculate all the composite numbers from 1 to n.", 10, 13, 0
two_instruct BYTE "N is the number you enter. That number has to be an integer,", 10, 13, 0
three_instruct BYTE "and in the range of 1 to 400.", 10, 13, 0


; ----- INPUT VALUES
; Prompts
int_prompt BYTE "How many composite numbers do you want to display? 1 to 400: ", 0
bad_prompt BYTE "Your Input was bad, try again", 10, 13, 0
; Constants
MAX_VAL = 400
MIN_VAL = 1


; ----- CALCULATE
x DWORD ?
spacer BYTE ?
count DWORD ?


; ----- SPACER_CHOICE
one_spacer BYTE ",     ", 0
ten_spacer BYTE ",    ", 0
hundo_spacer BYTE ",   ", 0


; ----- OUTRO
farewell BYTE "Hope you've had fun printing composites! Goodbye, ", 0

.code
main proc

    call intro

    call input

    call calculate

    call outro

    invoke ExitProcess, 0
main endp

; Greet the User and get their name
intro proc

    ; Greet
    mov edx, OFFSET title_prompt
    call WriteString

    ; Prompt for username
    mov edx, OFFSET name_prompt
    call WriteString
    mov edx, OFFSET username
    mov ecx, SIZEOF username
    call ReadString
    mov edx, OFFSET user_greet
    call WriteString
    mov edx, OFFSET username
    call WriteString
    call Crlf


    ; Intruct the User
    mov edx, OFFSET one_instruct
    call WriteString
    mov edx, OFFSET two_instruct
    call WriteString
    mov edx, OFFSET three_instruct
    call WriteString
intro endp


; Get User's Input. Validate if Between Min and Max
input proc

    ; Prompt for user input
    I1:
        mov edx, OFFSET int_prompt
        call WriteString
        mov eax, 0
        call ReadInt
        call Crlf


        ; Check if less than MAX_VAL
        cmp eax, MAX_VAL
        JA I4
        JMP I3


        I3:
            ; Check if greater than MIN_VAL
            cmp eax, MIN_VAL
            JB I4
            JMP I2


        I4:
            ; EAX is incorrect
            ; Loop
            mov edx, OFFSET bad_prompt
            call WriteString
    loop I1

    ; Good Input
    ; Set n_comps to eax
    I2:
        mov n_comps, eax
        mov eax, 0
    RET 
input endp


; X is not divisible by 2 or 3, is prime
calculate proc

    mov ecx, 400
    mov x, 1

    C1:
        ; Check if X is less than, or equal to, 3
        cmp x, 3
        JBE C2

        C4:
            ; Check if X is divisible by 2
            mov edx, 0
            mov eax, x
               mov ebx, 2
            div ebx
            cmp edx, 0
            JE C3

            ; Check if X is divisible by 3
            mov edx, 0
            mov eax, x
               mov ebx, 3
            div ebx
            cmp edx, 0
            JNE C2

        C3:
            ; Is composite, Print number
            mov eax, x
            cmp count, 10
            JNE C5

            call Crlf
            mov count, 0

            C5:
                call WriteInt
                call spacer_choice
                mov edx, OFFSET spacer
                call WriteString
                add count, 1

        C2:
            ; RET if X == N
            mov eax, n_comps
            cmp x, eax
            JNE C6

            RET

            C6:
                ; Increment and LOOP
                add x, 1
            LOOP C1
calculate endp


; Define the spacer based on the places in x
spacer_choice proc

    cmp x, 10
    JB SC1

    cmp x, 100
    JB SC2

    mov edx, OFFSET hundo_spacer
    JMP SC3

    SC1:
        mov edx, OFFSET one_spacer
        JMP SC3

    SC2:
        mov edx, OFFSET ten_spacer
        JMP SC3

    SC3:
        mov spacer, OFFSET edx
        RET
spacer_choice endp


; Say Goodbye
outro proc

    mov edx, OFFSET farewell
    call WriteString
    mov edx, OFFSET username
    call WriteString
    call Crlf
outro endp

end main

【问题讨论】:

    标签: string assembly cpu-registers masm32 irvine32


    【解决方案1】:

    我在 spacer 的末尾添加了 ASCII 水平制表符 9,这样就不再需要 spacer_choice 过程了。

    Spacer 现在声明为:spacer BYTE ",", 9, 0

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2021-03-22
      • 1970-01-01
      • 2019-07-31
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      相关资源
      最近更新 更多