zoukankan      html  css  js  c++  java
  • NASM汇编学习系列(4)——获取命令行参数

    说明

    1. 本学习系列代码几乎完全摘自:asmtutor.com,如果英文可以的(也可以用谷歌浏览器翻译看),可以直接看asmtutor.com上的教程
    2. 本学习系列目录地址:https://www.cnblogs.com/whuwzp/p/nasm_contents.html
    3. 系统环境搭建:(我用的是ubuntu18.04.4 server,安装gcc、g++)
    sudo apt install nasm
    sudo apt install gcc-multilib
    

    0. 概览

    1. 承前:无
    2. 启后:本节,获取命令行参数。

    1. 命令行参数传递约定

    1. 命令行参数都保存在栈上(和c语言的参数传递保持一致,参数在栈上)
    2. 命令行参数构成类似main(int argc, char** argv)
      • 第一个参数:命令行参数的个数(包含exe名字这个参数)
      • 第二个参数:exe自身的名字
      • 第三个参数:输入的第一个参数
      • 第四个参数:输入的第二个参数
      • ...

    2. 获取命令行参数

    以下代码摘自:https://asmtutor.com/#lesson8

    functions.asm是之前的。

    %include        'functions.asm' ; 这个就是之前的
    SECTION .text
    global  _start
     
    _start:
     
        pop     ecx             ; 弹出第一个参数到ecx,即参数个数
     
    nextArg:
        cmp     ecx, 0h         ; check to see if we have any arguments left
        jz      noMoreArgs      ; if zero flag is set jump to noMoreArgs label (jumping over the end of the loop)
        pop     eax             ; 弹出下一个参数到eax,然后打印
        call    sprintLF        ; call our print with linefeed function
        dec     ecx             ; 递减ecx,看看剩下的参数个数
        jmp     nextArg         ; jump to nextArg label
     
    noMoreArgs:
        call    quit
    

    下面摘自https://asmtutor.com/#lesson8的测试结果:

    ~$ ./helloworld-args "This is one argument" "This is another" 101
    ./helloworld-args
    This is one argument
    This is another
    101
    
  • 相关阅读:
    JavaScript中双叹号“!!”作用
    JavaScript两个变量的值交换的多种方式
    自定义npm包——typeScript版本
    自定义npm包的创建、发布、更新和撤销
    vuex概念总结及简单使用实例
    详解javascript: void(0);
    面向对象的CSS
    vue指令概览
    实现水平居中的办法
    C#分块下载文件
  • 原文地址:https://www.cnblogs.com/whuwzp/p/nasm_cmdline.html
Copyright © 2011-2022 走看看