【问题标题】:Add 2 numbers and print the result using Assembly x86添加 2 个数字并使用 Assembly x86 打印结果
【发布时间】:2015-04-15 23:12:16
【问题描述】:

我是一名新手 Assembly x86 Learner,我想添加两个数字 (5+5) 并将结果打印在屏幕上。

这是我的代码:

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

请指正

【问题讨论】:

  • 写系统调用的参数应该是一个字符串。
  • 你的意思是我们不能使用 write 系统调用来打印一个整数?
  • 可以,但不能直接。
  • 你能帮我做吗?
  • 您必须将整数转换为字符串。参见例如stackoverflow.com/questions/25064565/…

标签: assembly x86 addition


【解决方案1】:

另一种将无符号整数转换为字符串并写入的方法:

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

【讨论】:

猜你喜欢
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多