访问和添加结构数组

时间:2014-12-01 07:05:03

标签: assembly x86 structure

我试图访问我的结构数组,它似乎运行不正常,这是我的代码,试图根据字符串&#39搜索结构数组;姓名&#39 ;.

    menu db 10, '------MENU------', 10, '1. Add Student', 10, '2. Delete Student', 10, '3. Delete All', 10, '4. Search Student', 10, '5. Display All', 10, '6. Exit', 10 
menulen equ $-menu
namep db 'Enter name: '
nameplen equ $-namep
fnamep db 'Enter firstname: '
fnameplen equ $-fnamep
lnamep db 'Enter lastname: '
lnameplen equ $-lnamep
agep db 'Enter age: '
ageplen equ $-agep
unitsp db 'Enter units enrolled: '
unitsplen equ $-unitsp
fullp db 'Sorry, the record is already full.', 10
fullplen equ $-fullp
record db '----Student Record----', 10
recordlen equ $-record
deleteallp db '----Deleted all Record----', 10
deleteallplen equ $-deleteallp
deleteerrorp db '----Student not in the record----', 10
deleteerrorplen equ $-deleteerrorp
delete db 'Deleted ', 10
deletelen equ $-delete
space db ' '
spacelen equ $-space
newline db '', 10
newlinelen equ $-newline
printfname db 'First name: '
printfnamelen equ $-printfname
printlname db 'Last name: '
printlnamelen equ $-printlname
printage db 'Age: '
printagelen equ $-printage
printunits db 'Number of units enrolled: '
printunitslen equ $-printunits
student equ 88
firstname equ 0
lastname equ 40
age equ 80
units equ 84
array_size equ 5


choice resb 1
x resb array_size*student
size resb 1
temp resb 1
string resb 40

    mov eax, 4
    mov ebx, 1
    mov ecx, namep
    mov edx, nameplen
    int 80h

    mov eax, 3
    mov edx, 0
    mov ecx, string
    mov edx, 80
    int 80h

    mov byte[temp], 0

    getstudentloop:
        mov al, byte[size]
        cmp byte[temp], al
        je studentnotfound

        mov esi, string
        mov ecx,esi
        imul ecx,[temp]
        mov edi, [ecx+x+student+firstname]

        cld 
        repe cmpsb
        dec esi
        dec edi

        jz studentdelete

        inc byte[temp]
        jmp getstudentloop

    studentdelete:
        mov ecx,esi
        imul ecx,[temp]
        mov word[ecx+x+student+firstname], 0

        mov ecx,esi
        imul ecx,[temp]
        mov word[ecx+x+student+lastname], 0

        mov ecx,esi
        imul ecx,[temp]
        mov word[ecx+x+student+age], 0

        mov ecx,esi
        imul ecx,[temp]
        mov word[ecx+x+student+units], 0

        mov eax, 4
        mov ebx, 1
        mov ecx, delete
        mov edx, deletelen
        int 80h

        dec byte[size]

        jmp menustart
    studentnotfound:
        mov eax, 4
        mov ebx, 1
        mov ecx, deleteerrorp
        mov edx, deleteerrorplen
        int 80h

        jmp menustart

我无法获取字符串名字,所以当我比较时,它会失败。 我也不确定如何添加到我的结构数组中,因为它会覆盖我之前添加的结构,将整个数组转换为我上次添加的元素。我不确定我是否正确行事。

    cmp byte[size], 5
    jge full

    mov eax, 4
    mov ebx, 1
    mov ecx, fnamep
    mov edx, fnameplen
    int 80h

    mov eax, 3
    mov ebx, 0
    mov ecx,esi
    imul ecx,[size]                  
    lea ecx,[ecx+x+student+firstname]
    mov edx, 20
    int 80h

    mov eax, 4
    mov ebx, 1
    mov ecx, lnamep
    mov edx, lnameplen
    int 80h

    mov eax, 3
    mov ebx, 0
    mov ecx,esi
    imul ecx,[size]                  
    lea ecx,[ecx+x+student+lastname]
    mov edx, 20
    int 80h

    mov eax, 4
    mov ebx, 1
    mov ecx, agep
    mov edx, ageplen
    int 80h


    mov eax, 3
    mov ebx, 0
    mov ecx,esi
    imul ecx,[size]                    
    lea ecx,[ecx+x+student+age]
    mov edx, 3
    int 80h


    mov eax, 4
    mov ebx, 1
    mov ecx, unitsp
    mov edx, unitsplen
    int 80h


    mov eax, 3
    mov ebx, 0
    mov ecx,esi
    imul ecx,[size]                  
    lea ecx,[ecx+x+student+units]
    mov edx, 3
    int 80h


    add byte[size], 1
    jmp menustart

1 个答案:

答案 0 :(得分:0)

一个。你忘了初始化SIZE!你只定义了它......

湾您已将SIZE和TEMP定义为字节。

size resb 1
temp resb 1

但是下面的代码需要一个双字。当涉及内存操作数时,这是IMUL指令的要求!

imul ecx,[temp]
imul ecx,[size]

所以简单地写一下

size resd 1
temp resd 1

mov dword[size], 0
mov dword[temp], 0