简体   繁体   English

linux x64加法程序

[英]linux x64 addition program

I am making an addition program using x64 assembly, but it does not display a value when run (compiled with nasm, elf64). 我正在使用x64汇编程序编写附加程序,但是运行时(与nasm,elf64编译)不显示任何值。

section .text
global _start

_start:
mov rax, 0
add rax, [num1B]
add rax, [num2B]
mov [result], rax
mov rsi, [result]
;mov    rdx, 8  
mov rax, 4
mov rdi, 1
int 80h

mov rax, 1
mov rdi, 0
int 080h


section .data

num1B: dq 0Ah
num2B: dq 0Ah
result: dq 00h

Does anyone know why this is not displaying anything 有谁知道为什么这没有显示任何东西

1.later use printf instead of interrupts, much better, 2.why put values of numB1 and numB2 instead of their location. 1.后来使用printf代替中断,效果更好,2.为什么用numB1和numB2的值代替它们的位置。 Use: mov rax, numB1. 使用:mov rax,numB1。 3.in 64bit nasm assembly you use the: rdi, rsi, rbx, rcx,... Registers for putting in values for interrupts. 3.在64位nasm汇编中,您使用: rdi, rsi, rbx, rcx,...用于放置中断值的寄存器。 For example: 例如:

mov rdi, 01
mov rsi, 00
syscall
  1. DON't USE int0x80 !, for more-portability use syscall and besides int 0x80 didn't work on my system. 不要使用int0x80 !,因为要具有更多的可移植性,请使用syscall ,此外int 0x80在我的系统上不起作用。

Hope it helps, Correct me if I'm wrong. 希望对您有所帮助,如果我错了,请纠正我。

Looks like you want to print 'result' to stdout and you are using the 32-bit system call values. 看起来您想在标准输出上打印“结果”,并且您正在使用32位系统调用值。

In 64-bit linux the system call for write is 1 and you would write to stdout like this... att&t syntax: first strip values in %rax and push them byte by byte on stack, say %rax holds the value 0x7ffffff8: 在64位linux中,写入的系统调用为1,您将像这样写到stdout ... att&t语法:首先剥离%rax中的值,并将它们逐字节推入堆栈,例如%rax保持值为0x7ffffff8:

mov $0xa, %rbx        # divisor

nibble:
xor %rdx, %rdx        # will hold bytes values you need
div %rbx, %rax
push %rdx             # save remainder
inc  %r8              # count digit, write seems to trash %rcx
cmp  $0,  %rax        # done?
jne  nibble           # no, get another digit

#set up for write to stdout

mov $1, %rax          # sys_call for write
mov $1, %rdi          # write to stdout
mov $result, %rsi     # addr. of value to print

# now get values from stack, make ascii and write to stdout

decimal:
pop %rdx              # get digit off stack
add $0x30, %dl        # make ascii printable
movb %dl, result      # load addr. with value
mov $1, %rdx          # print 1 byte
syscall

dec %r8
jnz decimal           # go till %r8 is zero 

You just need to set up a 1 byte data holder for digits,either in data section: 您只需要在数据部分中为数字设置一个1字节的数据保存器:

.section .data
    result:
    .byte 0             # reserves 1 byte and inits to 0

or the uninitialized data area: 或未初始化的数据区域:

.section .bss
    .lcomm result, 1    # reserves 1 byte 

I'm sure there are better ways to do this, should give you some ideas though. 我敢肯定,有更好的方法可以做到这一点,但是应该给您一些想法。

Get a 64-bit system call list, they have changed quite a lot from the 32-bit calls. 获取一个64位系统调用列表,它们与32位调用相比已发生了很大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM