为什么 sub eax, '0' 用于添加而不是添加? [英] Why sub eax, '0' for adding instead of just add?

查看:48
本文介绍了为什么 sub eax, '0' 用于添加而不是添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在尝试学习一些汇编,我看到了一个加法的例子,但我真的不明白一件事:

So, I've been trying to learn some assembly and I saw an example of an addition but I don't really understand one thing:

section .text
    global _start       
_start:                     
    mov eax, '3'
    sub eax, '0'
    mov ebx, '4'
    sub ebx, '0'
    add eax, ebx
    add eax, '0'
    mov [sum], eax
    mov ecx, msg
    mov edx, len
    mov ebx, 1
    mov eax, 4
    int 0x80
    mov ecx, sum
    mov edx, 1
    mov ebx, 1
    mov eax, 4
    int 0x80
    mov eax, 1
    int 0x80
section .data
msg db  'The sum is:',0xA, 0xD
len equ $ - msg 
segment .bss
sum resb

我理解除了sub eax, '0'

我的意思是结果应该是 -7 因为当它执行 sub eax, '0' 时,它会反转数字...

I mean the result should be -7 because when it does sub eax, '0' it inverses the number...

推荐答案

... 因为当它执行 sub eax, '0' 时它反转了 num

... because when it does sub eax, '0' it inverses the num

减法sub eax, '0'AL中的字符转换成对应的数字 0-9.仅此而已.

The subtraction sub eax, '0' converts the character in AL into the corresponding number 0-9. Nothing more.

应该是这样写的:

sub al, '0'

指令add al, '0'以同样的方式将AL中的数字(0-9范围内)转换为准备输出的字符.

In the same manner will the instruction add al, '0' convert the number in AL (in the range 0-9) into a character that's ready for outputting.

我的意思是结果应该是-7

I mean the result should be -7

如果您运行该程序,您会看到输出为正数 7.

If you run the program, you'll see that the output will be a positive 7.

这篇关于为什么 sub eax, '0' 用于添加而不是添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆