无法在 Assembly Nasm 的子程序 calculate_inventory 中计算正确的总库存统计数据

问题描述 投票:0回答:0

当主程序调用子程序 calculate_inventory 计算最终统计数据并打印时,所有结果均为 0。如下截图。

  %define MAX_BUFFER 25
   %define EOF -1

   ;; Here is out struct definition
   ;; something akin to what we would put in a .h file
 
   struc product
   .productID:   resd 1
   .productName: resq 1
   .price: resd 1
   .cost:  resd 1
   .quantity resd 1
   .redPtr   resq 1
   .size:
   endstruc
  

segment .data
   welcome:    db 10,"Welcome to Smith's Seed Inventory Control System!",10,"Please have product number, product name, price, cost, and quantity information ready.", 10, 0

   promptNum:  db 10,"Start entering details or press (ctrl d) to finish",10,"Product Number: ",0
   promptNam:  db "Product Name: ",0
   promptPri:  db "Product Price: ",0
   promptCos:  db "Product Cost: ",0
   promptQua:  db "Product Quantity: ",0

   inventory:  db 10,10,"Here is the current store inventory:",10,0
   outputA:   db "Product Number: %d",10,0
   outputB:   db "Product Name %s",10,0
   outputC:   db "Product Price: $%d",10,0
   outputD:   db "Product Cost: $%d",10,0
   outputE:   db "Product Quantity: %d",10,10,0
   intFormat: db "%d",0
   strFormat: db "%s",0
   
   outputF: db "Total Amount of Inventory: %d", 10, 0
   outputG: db "Total Cost: $%d", 10, 0
   outputH: db "Total Value: $%d", 10, 0

segment .bss
   productDB:   resb product.size
   intInput:    resd 1
   stringInput: resb MAX_BUFFER
   head: resb product.size

segment .text
   global asm_main
   extern printf, scanf, malloc, free, strncpy, strnlen
   
asm_main:
  mov   rdi, welcome          ; Print welcome message
  xor rax, rax
  call  printf

  call read_inventory
  mov    r13, rax               ; store return value

  mov   rdi, inventory          ; Print inventory message
  xor rax, rax
  call  printf

  mov rbx, r13                  ; pass one argument head node
  call print_inventory
  
  mov rbx, r12           ; pass head node to calculate_inventory function
  call calculate_inventory

  ret

read_inventory:
   mov rdi, product.size                      ; allocate memory for head node
   mov rsi, 1
   call malloc

   mov rbx, rax
   mov qword [rbx+product.redPtr], 0 
   mov r12, rax                               ; store head in r12
loopA:
   mov   rdi, promptNum                       ; Read in product number
   xor rax, rax
   call  printf
   mov   rdi, intFormat
   mov   rsi, intInput
   xor rax, rax
   call  scanf

   cmp eax, EOF
   je done
   
   mov   eax, [intInput]
   mov   [rbx+product.productID], eax


   mov   rdi, promptNam                        ; Read in product name
   xor rax, rax
   call  printf
   mov   rdi, strFormat
   mov   rsi, stringInput
   call  scanf

   mov   rdi, stringInput       ; Get string size and malloc space for string
   mov   rsi, MAX_BUFFER
   call  strnlen
   inc   rax
   mov   r13, rax

   mov   rdi, rax
   mov   rsi, 1
   call  malloc

   mov   [rbx+product.productName], rax    ; Copy first name string to struct
   mov   rdi, rax
   mov   rsi, stringInput
   mov   rdx, r13
   call  strncpy

  
   mov   rdi, promptPri                        ; Read in product price
   xor rax, rax
   call  printf
   mov   rdi, intFormat
   mov   rsi, intInput
   xor rax, rax
   call  scanf

   mov   eax, [intInput]
   mov   [rbx+product.price], eax

   mov   rdi, promptCos                        ; Read in product cost
   xor rax, rax
   call  printf
   mov   rdi, intFormat
   mov   rsi, intInput
   xor rax, rax
   call  scanf

   mov   eax, [intInput]
   mov   [rbx+product.cost], eax

   mov   rdi, promptQua                       ; Read in product quantity
   xor rax, rax
   call  printf
   mov   rdi, intFormat
   mov   rsi, intInput
   xor rax, rax
   call  scanf

   mov   eax, [intInput]
   mov   [rbx+product.quantity], eax

   mov rdi, product.size                      ; allocate memory for next node
   mov rsi, 1
   call malloc

   mov [rbx+product.redPtr], rax
   mov rbx, rax
   mov qword [rbx+product.redPtr], 0 

   jmp loopA
done:
   mov rax, r12                              ; return head node
   ret


print_inventory:

LoopB:
   mov rax, qword [rbx+product.redPtr]
   cmp rax, 0
   je endLoopB
   mov   rdi, outputA
   mov   rsi,  [rbx+product.productID]
   xor rax, rax
   call  printf

   mov   rdi, outputB
   mov   rsi,  [rbx+product.productName]
   xor rax, rax
   call  printf

   mov   rdi, outputC
   mov   rsi,  [rbx+product.price]
   xor rax, rax
   call  printf

   mov   rdi, outputD
   mov   rsi,  [rbx+product.cost]
   xor rax, rax
   call  printf

   mov   rdi, outputE
   mov   rsi,  [rbx+product.quantity]
   xor rax, rax
   call  printf

   mov rbx, [rbx+product.redPtr]

   Jmp LoopB

endLoopB:
   ret
   
calculate_inventory:
   mov rbx, rdi
   xor r15, r15 ; Total quantity
   xor r14, r14 ; Total cost
   xor r13, r13 ; Total value

calcLoop:
   mov rax, [rbx+product.quantity]   ; Get quantity of current product
   add r15, rax                      ; Add to total quantity

   mov rax, [rbx+product.cost]       ; Get cost of current product
   imul rax, [rbx+product.quantity]  ; Multiply by quantity
   add r14, rax                      ; Add to total cost

   mov rax, [rbx+product.price]      ; Get price of current product
   imul rax, [rbx+product.quantity]  ; Multiply by quantity
   add r13, rax                      ; Add to total value

   mov rbx, [rbx+product.redPtr]     ; Get next product
   cmp rbx, 0                        ; Check if end of list
   jne calcLoop

   mov rdi, outputF                  ; Print total quantity
   mov rsi, r15
   xor rax, rax
   call printf

   mov rdi, outputG                  ; Print total cost
   mov rsi, r14
   xor rax, rax
   call printf

   mov rdi, outputH                  ; Print total value
   mov rsi, r13
   xor rax, rax
   call printf

   ret

我不确定是什么问题。我将参数传递给函数,但所有输出仍然为零。任何帮助都会被拒绝。

程序的作用——这段代码实现了一个商店的基本库存管理系统。该程序允许用户输入有关各种产品的详细信息,例如产品 ID、名称、价格、成本和数量。数据存储在链表数据结构中,每个节点代表一个产品。用户输入所有数据后,程序会打印出商店的当前库存,包括每个产品的详细信息,如产品 ID、名称、价格、成本和数量。最后,程序计算并打印出存货的总库存量、总成本、总价值。

assembly struct linked-list nasm
© www.soinside.com 2019 - 2024. All rights reserved.