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
    
  • 相关阅读:
    JDBC批处理数据
    JSP+Servlet 无数据库模拟登录过程
    idea常用插件
    如何破解IntelliJ IDEA2018教程
    java在线工具
    mysql快捷修改密码
    jdk1.8新特性
    java基础感觉白学了
    论JDK源码的重要性:一道面试题引发的无限思考
    数组算法经典实例
  • 原文地址:https://www.cnblogs.com/whuwzp/p/nasm_cmdline.html
Copyright © 2011-2022 走看看