新增2号和打印使用的x86大会结果 [英] Add 2 numbers and print the result using Assembly x86

查看:226
本文介绍了新增2号和打印使用的x86大会结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手大会的x86学习者,我想添加两个数字(5 + 5),并在屏幕上打印的结果。

I'm a novice Assembly x86 Learner, and i want to add two numbers (5+5) and print the result on the screen.

这是我的code:

global _start

section .text
_start:
    mov eax, 5
    mov ebx, 5
    add eax, ebx
    push eax
    mov eax, 4 ; call the write syscall
    mov ebx, 1 ; STDOUT
    pop ecx    ; Result
    mov edx, 0x1
    int 0x80

    ; Exit
    mov eax, 0x1
    xor ebx, ebx
    int 0x80

纠正我,请

推荐答案

另一种方法为无符号整数转换为字符串,并把它写:

Another approach to convert an unsigned integer to a string and write it:

section .text
global _start
_start:

    mov eax, 1234567890
    mov ebx, 5
    add eax, ebx

    ; Convert EAX to ASCII and store it onto the stack
    sub esp, 16             ; reserve space on the stack
    mov ecx, 10
    mov ebx, 16
    .L1:
    xor edx, edx            ; Don't forget it!
    div ecx                 ; Extract the last decimal digit
    or dl, 0x30             ; Convert remainder to ASCII
    sub ebx, 1
    mov [esp+ebx], dl       ; Store remainder on the stack (reverse order)
    test eax, eax           ; Until there is nothing left to divide
    jnz .L1

    mov eax, 4              ; SYS_WRITE
    lea ecx, [esp+ebx]      ; Pointer to the first ASCII digit
    mov edx, 16
    sub edx, ebx            ; Count of digits
    mov ebx, 1              ; STDOUT
    int 0x80                ; Call 32-bit Linux

    add esp, 16             ; Restore the stack

    mov eax, 1              ; SYS_EXIT
    xor ebx, ebx            ; Return value
    int 0x80                ; Call 32-bit Linux

这篇关于新增2号和打印使用的x86大会结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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