[英]How to add a member in an array of structure x8086
I'm still having trouble with structures in assembly and I'm stuck with adding members inside an array of structure. 我在组装结构时仍然遇到麻烦,并且无法在结构数组中添加成员。 I'm able to add members now, but instead of just adding one in an array of structure, it overwrites all the members that I've added earlier. 我现在可以添加成员,但是它不只是在结构数组中添加成员,它会覆盖我之前添加的所有成员。 So all the members of the array will be similar to the last one that I added. 因此,数组的所有成员都将与我添加的最后一个成员相似。
Here's the data initialization 这是数据初始化
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
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
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 94
firstname equ 0
firstnamelen equ 40
lastname equ 42
lastnamelen equ 82
age equ 86
units equ 90
array_size equ 5
choice resb 1
x resb array_size*student
size resb 1
temp resb 1
Here's the adding to the structure part. 这是结构部分的添加。
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
imul ecx,esi,size
lea ecx,[ecx+esi+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
imul ecx,esi,size
lea ecx,[ecx+esi+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
imul ecx,esi,size
lea ecx,[ecx+esi+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
imul ecx,esi,size
lea ecx,[ecx+esi+x+student+units]
mov edx, 3
int 80h
add byte[size], 1
jmp menustart
And here's the displaying all part 这是展示的全部
mov eax, 4
mov ebx, 1
mov ecx, record
mov edx, recordlen
int 80h
mov al, byte[size]
mov [temp], al
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, newlinelen
int 80h
displayloop:
cmp byte[temp], 0
je menustart
mov eax, 4
mov ebx, 1
mov ecx, printfname
mov edx, printfnamelen
int 80h
mov eax, 4
mov ebx, 1
imul ecx,esi,temp
lea ecx, [ecx+x+student+firstname]
mov edx, 20
int 80h
mov eax, 4
mov ebx, 1
mov ecx, printlname
mov edx, printlnamelen
int 80h
mov eax, 4
mov ebx, 1
imul ecx,esi,temp
lea ecx, [ecx+x+student+lastname]
mov edx, 20
int 80h
mov eax, 4
mov ebx, 1
mov ecx, printage
mov edx, printagelen
int 80h
mov eax, 4
mov ebx, 1
imul ecx,esi,temp
lea ecx, [ecx+x+student+age]
mov edx, 3
int 80h
mov eax, 4
mov ebx, 1
mov ecx, printunits
mov edx, printunitslen
int 80h
mov eax, 4
mov ebx, 1
imul ecx,esi,temp
lea ecx, [ecx+x+student+units]
mov edx, 3
int 80h
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, newlinelen
int 80h
dec byte[temp]
jmp displayloop
These instructions need to change in your code 这些说明需要更改您的代码
imul ecx,esi,size
imul ecx,esi,temp
You are multiplying with the address of these variables in stead of their values! 您要乘以这些变量的地址而不是它们的值! Ideally you would use the following : 理想情况下,您将使用以下内容:
imul ecx,esi,[size]
imul ecx,esi,[temp]
But these operands are not possible!!! 但是这些操作数是不可能的!
I suggest using a construct like : 我建议使用类似的构造:
mov ecx,esi
imul ecx,[size]
mov ecx,esi
imul ecx,[temp]
Also change the size of both variables to DWORD in stead of BYTE. 还将两个变量的大小都改为BYTE,而不是DWORD。 IMUL requires it. IMUL需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.