简体   繁体   English

将值插入到程序集MASM32中的数组中

[英]Inserting values into array in assembly MASM32

I'm having trouble figuring out how to put a value I currently have in register EAX into an array. 我在弄清楚如何将当前在寄存器EAX中拥有的值放入数组中时遇到麻烦。

The basic function of this program is to take the dates array, convert them to unique numbers, BubbleSort them, and convert them back. 该程序的基本功能是获取date数组,将其转换为唯一数字,然后对BubbleSort进行排序,然后将其转换回。

I put the values I find with "datetonum" into EAX, and I want to store those values I find into my array named ra . 我将用“ datetonum”找到的值放入EAX中,我想将找到的值存储到名为ra数组中。

I can't seem to figure out how to do something that. 我似乎无法弄清楚该怎么做。
Seems like it should be fairly simple? 似乎应该很简单?

Thanks in advance! 提前致谢!

include \masm32\include\masm32rt.inc
.data
  dates  BYTE "23-JUL-2010", 0, "23-JAN-2010", 0, "23-JUL-2009", 0, "31-JUL-2012", 0, "05-MAR-2010", 0
  months BYTE "JAN",0,"FEB",0,"MAR",0,"APR",0,"MAY",0,"JUN",0,"JUL",0,"AUG",0,"SEP",0,"OCT",0,"NOV",0,"DEC",0
  nDates DWORD 4
  ra     DWORD 1,0,0,0,0

.code
start:
    lea  EAX, dates     ; create pointer to beginning of dates array
    push EAX
    call datetonum

    ;-------------------
    ;put value currently in EAX into array to be sorted
    ;-------------------
    print "Array to be sorted contents: "
    print chr$(13,10)


    lea ECX, ra
    mov [ECX + nDates], EAX
    print ra
    print chr$(13,10)

Here's the datetonum function 这是datetonum函数

;param1 (date string) = address of 12 byte date
datetonum:
    enter 4, 0                    ; [EBP - 4] holds 4 byte temp variable
    mov  EBX, [EBP + 8]           ; pointer to entire date string
    lea  ESI, [EBX]               ; pointing to day part of date
    lea  EDI, [EBP - 4]           ; pointing to address of local variable to store day string
    mov  ECX, 2
    cld
    rep  movsb
    mov  EDX, 0
    mov  [EDI], EDX               ; add null terminator

    lea  EDX, [EBP - 4]
    mov  EAX, sval(EDX)           ; convert day string to int
    push EAX                      ; push EAX to stack


    ; extract month from date
    lea  ESI, [EBX + 3]           ; pointing to month part of date
    lea  EDI, [EBP - 4]           ; pointing to address of local variable to store month string
    mov  ECX, 3
    cld
    rep  movsb
    mov  EDX, 0
    mov  [EDI], EDX               ; add null terminator



    ; debug print of month string
    pushad
    lea  EDX, [EBP - 4]
    print EDX
    print chr$(9)                 ; print a tab character
    print chr$(13,10)
    popad

    ; find month number
    sub ESI, ESI
    lea EDX, [EBP - 4]
    mov EAX, [EDX]
    mov ECX, 12
search_top:
    lea EDX, [months + ESI * 4]
    mov EBX, [EDX]
    inc ESI
    cmp EAX, EBX
    loopne search_top

    mov EDX, ESI                    ; result is in ESI

    pop EAX                         ; pop EAX off the stack
    mov AH, DL                      ; copy the month int into AH
    push EAX                        ; push EAX to stack

    ; convert year chars to 2 byte int
    mov EBX, [EBP + 8]
    lea ESI, [EBX + 7]              ; pointing to year part of date
    lea EDI, [EBP - 4]              ; pointing to address of local variable to store year string
    mov ECX, 4
    cld
    rep movsb
    mov EDX, 0
    mov [EDI], EDX                  ; add null terminator

    lea EDX, [EBP - 4]
    mov EDX, sval(EDX)              ; convert year string to int

    pop EAX                         ; pop EAX off the stack
    mov EBX, EAX                    ; copy EAX (contains month in AH and day in AL) to EBX
    mov EAX, EDX                    ; copy year to EAX
    shl EAX, 16                     ; shift the year over to high 16 bits in EAX
    mov AX, BX                      ; copy the month and day into low 16 bits in EAX

    ;print EAX ; this crashes the proc
    print str$(EAX)
    print chr$(13,10)

    leave
    ret 4
    exit

If your dates are stored in an array, and you want to use another array to store the dates converted to numbers, you will need at least one loop. 如果您的日期存储在一个数组中,并且您想使用另一个数组存储转换为数字的日期,则将需要至少一个循环。 You can use index registers (ESI, EDI) to point to both arrays, then use one register to read the value from one array, and the other register to move the value into the other array. 您可以使用索引寄存器(ESI,EDI)指向两个数组,然后使用一个寄存器从一个数组中读取值,并使用另一个寄存器将值移到另一个数组中。

Let's imagine you already have next 3 procedures : 假设您已经有以下3个过程:

  • num2date : converts a num in EAX back into a date (the opposite of datetonum ). num2date :将EAX中的num转换回日期(与datetonum相反)。 This date is stored in a variable date that is declared like this : date BYTE "DD-MMM-AAAA" . 此日期存储在这样声明的可变date中: date BYTE "DD-MMM-AAAA"
  • bubble_sort : sorts the numbers in array ra . bubble_sort :对数组ra的数字进行排序。
  • transfer_date : takes the 11 bytes of variable date and move them into the position pointed by ESI ( ESI is pointing somewhere inside array dates ). transfer_date :取11个字节变量的date ,并将其移动到由所指示的位置ESIESI被阵列内某处指向dates )。

This could be the logic of your code : 这可能是代码的逻辑:

start:
;▼ NEXT LOOP CONVERTS ALL DATES TO NUMBERS ▼
    lea ESI, dates      ;POINTER TO ARRAY "DATES".
    lea EDI, ra         ;POINTER TO ARRAY "RA".
    mov ECX, 5          ;LENGTH OF ARRAY "DATES".
dates2numbers:
    push ECX ESI EDI    ;PRESERVE REGISTERS.
    mov  EAX, [ESI]     ;CURRENT DATE.
    push EAX
    call datetonum      ;CONVERT CURRENT DATE TO A NUMBER.
    pop  EAX            ;GET THE NUMBER.
    pop  EDI ESI ECX    ;RESTORE REGISTERS.
    mov  [EDI], EAX     ;STORE NUMBER IN "RA".
    add  ESI, 12        ;NEXT ITEM IN ARRAY "DATES" (12 BYTES EACH).
    add  EDI, 4         ;NEXT ITEM IN ARRAY "RA" (4 BYTES EACH).
    loop dates2numbers

    call bubble_sort    ;◄■■■ SORT ARRAY "RA".

;▼ NEXT LOOP CONVERTS ALL NUMBERS TO DATES ▼
    lea ESI, dates      ;POINTER TO ARRAY "DATES".
    lea EDI, ra         ;POINTER TO ARRAY "RA".
    mov ECX, 5          ;LENGTH OF ARRAY "RA".
numbers2dates:
    push ECX ESI EDI    ;PRESERVE REGISTERS.
    mov  EAX, [EDI]     ;CURRENT NUMBER.
    call num2date       ;CONVERTS EAX TO A DATE IN VARIABLE "DATE".
                        ;VARIABLE "DATE" LOOKS LIKE THIS : DATE BYTE "DD-MMM-AAAA"
                        ;"NUM2DATE" SHOULD PRESERVE ESI AND RESTORE IT
                        ;BECAUSE "TRANSFER_DATE" WILL NEED IT.
    call transfer_date  ;TRANSFER CONTENT OF VARIABLE "DATE" INTO THE
                        ;ARRAY "DATES" AT THE POSITION ESI IS POINTING TO.
    pop  EDI ESI ECX    ;RESTORE REGISTERS.
    add  ESI, 12        ;NEXT ITEM IN ARRAY "DATES" (12 BYTES EACH).
    add  EDI, 4         ;NEXT ITEM IN ARRAY "RA" (4 BYTES EACH).
    loop numbers2dates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM