© Jo Van Hoey 2019
J. Van HoeyBeginning x64 Assembly Programminghttps://doi.org/10.1007/978-1-4842-5076-1_21

21. Command Line

Jo Van Hoey1 
(1)
Hamme, Belgium
 

Sometimes you want to start a program at the command line using arguments that will be used by that program. This can be useful when developing your own CLI tools. System administrators use CLI tools all the time, because as a rule, CLI tools work faster for a knowledgeable user.

Accessing Command-Line Arguments

In the example program in Listing 21-1, we show how you can access command-line arguments within your assembly program. We keep it simple; we just find the arguments and print them.
;cmdline.asm
extern printf
section .data
     msg   db      "The command and arguments: ",10,0
     fmt   db      "%s",10,0
section .bss
section .text
     global main
main:
push rbp
mov  rbp,rsp
     mov   r12, rdi     ; number of arguments
     mov   r13, rsi     ; address of arguments array
;print the title
     mov   rdi, msg
     call  printf
     mov   r14, 0
;print the command and arguments
.ploop:               ; loop through the array and print
     mov   rdi, fmt
     mov   rsi, qword [r13+r14*8]
     call  printf
     inc   r14
     cmp   r14, r12   ; number of arguments reached?
     jl    .ploop
leave
ret
Listing 21-1

cmdline.asm

When executing this program, the number of arguments, including the program name itself, is stored in rdi. The register rsi contains the address of an array in memory, containing the addresses of the command-line arguments, with the first argument being the program itself. The use of rdi and rsi agrees with the calling conventions. Remember that we are working here on Linux and using the System V AMD64 ABI calling conventions; on other platforms, such as Windows, other calling conventions are used. We copy this information because rdi and rsi will be used later for printf.

The code loops through the argument array until the total number of arguments is reached. In the loop .ploop, r13 points to the array of arguments. The register r14 is used as an argument counter. In every loop, the address of the next argument is calculated and stored in rsi. The 8 in qword [r13+r14*8] refers to the length of the addresses pointed to: 8 bytes × 8 bits = 64-bit address. The register r14 is compared in every loop with r12, containing the number of arguments.

Figure 21-1 shows the output with some random arguments.
../images/483996_1_En_21_Chapter/483996_1_En_21_Fig1_HTML.jpg
Figure 21-1

cmdln.asm output

Debugging the Command Line

Currently, SASM cannot be used for debugging programs with command-line arguments; you will have to use GDB. The following is one way to do that:
gdb --args ./cmdline arg1 arg2 abc 5
break main
run
info registers rdi rsi rsp
You can verify with the previous instructions that rdi contains the number of arguments (including the command itself) and that rsi points to an address in high memory, even higher than the stack, as already hinted at in Chapter 8 (see Figure 8-7). Figure 21-2 shows the output of GDB.
../images/483996_1_En_21_Chapter/483996_1_En_21_Fig2_HTML.jpg
Figure 21-2

gdb cmdline output

In Figure 21-2 the array with the addresses of the arguments starts at 0x7fffffffde58. Let’s dig down more for the actual arguments. The address of the first arguments can be found with the following:
      x/1xg 0x7fffffffde58
Here we are asking for one giant word (8 bytes) in hexadecimal at address 0x7fffffffde58. Figure 21-3 shows the answer.
../images/483996_1_En_21_Chapter/483996_1_En_21_Fig3_HTML.jpg
Figure 21-3

GDB address of the first argument

Now let’s find out what sits at that address (Figure 21-4).
x/s 0x7fffffffe204
../images/483996_1_En_21_Chapter/483996_1_En_21_Fig4_HTML.jpg
Figure 21-4

GDB, the first argument

This is indeed our first argument, the command itself. To find the second argument, augment 0x7fffffffde58 with 8 bytes to 0x7fffffffde60, find the address of the second argument, and so on. Figure 21-5 shows the result.
../images/483996_1_En_21_Chapter/483996_1_En_21_Fig5_HTML.jpg
Figure 21-5

GDB, the second argument

This is how you can debug and verify command-line arguments.

Summary

In this chapter, you learned about the following:
  • How to access command-line arguments

  • How to use registers for command-line arguments

  • How to debug with command-line arguments

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.157.6