NASM Assembly x86-64 - 帮助循环命令行参数

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

assembly 对我来说是新手,我在尝试创建程序时遇到问题。该程序应该显示用户给出的参数数量以及用户输入的每个参数。当没有给出参数时,它还应该显示一条消息(通过不将程序调用语句计为参数)。我无法弄清楚如何循环参数,以便程序显示所有参数,无论用户给出多少个参数。有谁知道我该如何解决这个问题?谢谢!

代码如下:

%include "CPsub64.inc"

global main ;entry point for assembly
extern puts

section .text
main:
  push rdi  ;save to stack
  push rsi  ;save to stack

;show # of arguments
  mov rax, rdi
  call WriteDec



;put blank line
  mov rdi, blankline
  call puts

;if more than 1 argument is given then jump to great
  mov rcx, 1
  cmp rcx, rdi
  jg great

  jmp less
;return original values
great:
  pop rsi
  pop rdi


loop:
;show items
  push rdi          ;save to stack
  push rsi
  sub rsp, 8    ;align stack boundary
  add rsi, 8        ;move address to next item
  mov rdi, [rsi]  ;argument from command line
  call puts
cmp rsi, rdi
  jne loop
  add rsp, 8    ;realign boundary
  pop rsi            ;restore registers
  pop rdi

  jmp skip
  less:
  pop rsi
  pop rdi

;display noarg message
  mov rax, 1
  mov rsi, noarg
  mov rdx, noarglength
  syscall

  jmp end
skip:
end:


;display end message
  mov rax, 1
  mov rsi, endmessage
  mov rdx, endmessagelength
  syscall

  ret       ;close C functions


section .data
  blankline: db " ",0

  noarg: db 'No arguments found.',0,0ah 
  noarglength: equ ($-noarg)

  endmessage: db 'The program is completed',0,0ah
  endmessagelength: equ ($-endmessage)
assembly command-line-arguments nasm
1个回答
0
投票

你有没有让它工作过?

© www.soinside.com 2019 - 2024. All rights reserved.