2016-11-17 57 views
0

我在計算如何將當前在寄存器EAX中的值放入數組中時遇到問題。將值插入到程序集中的數組MASM32

這個程序的基本功能是取日期數組,將它們轉換爲唯一的數字,BubbleSort它們,並將它們轉換回來。

我把我發現的值與「datetonum」放到EAX中,我想將這些值存入我的數組ra

我似乎無法弄清楚該怎麼做。
似乎它應該相當簡單?

在此先感謝!

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) 

這裏的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 
+0

哪裏是'datetonum'的代碼? –

+0

@JoseManuelAbarcaRodríguez我可以包括它 - 但我不知道有必要看到完成我想要做的事情。它只是將日期中的一個日期轉換爲唯一的32位數字,並將其存儲在EAX中 – tay1392

+1

嗯,有必要將該代碼包含到您的問題中,因爲缺少[MCVE]而不能關閉_(http:// stackoverflow .COM /幫助/ MCVE)。我會高度建議它。 – zx485

回答

0

如果您的日期是存儲在一個數組,並要使用另一個陣列來存儲轉換爲數字的日期,您將需要至少一個循環。您可以使用索引寄存器(ESI,EDI)指向兩個數組,然後使用一個寄存器從一個數組讀取值,而另一個寄存器將值移入另一個數組。

讓我們想象一下,你已經擁有了未來3個程序:

  • num2date:在EAX一個NUM轉換回一個日期(的datetonum對面)。該日期存儲在變量date中,該變量聲明如下:date BYTE "DD-MMM-AAAA"
  • bubble_sort:對數組ra中的數字進行排序。
  • transfer_date:將變量date的11個字節移動到ESI指向的位置(ESI指向數組dates內的某處)。

這可能是你的代碼的邏輯:

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 
+0

'call datetonum' +'pop eax':作爲返回值在'eax'中,這看起來不是個好主意。另外OP使用'ret 4'來彈出參數,所以你不需要'彈出'。 – Ped7g

+0

所以,@ tay1392,我想我的回答沒有用,也沒有太複雜,是嗎?我在每條指令的右邊添加了解釋它的意見。我想告訴你將所有日期轉換爲數字,排序並將它們轉換回日期的邏輯。 –

+1

@JoseManuelAbarcaRodríguez這對Jose很有幫助。我感謝您花時間做到這一點。這有點複雜,因爲我真的對組裝並不滿意。但是你的意見和邏輯是有道理的。沒有最終讓我的任務工作,但哦。再次感謝。 – tay1392