0

I am a developer who uses high level languages, learning assembly language in my spare time. Please see the NASM program below:

section .data

section .bss

section .text
global main
main:
    mov eax,21
    mov ebx,9
    add eax,ebx
    mov ecx,eax
    mov eax,4
    mov ebx,1
    mov edx,4
    int 0x80
push ebp
mov ebp,esp

mov esp,ebp
pop ebp
ret

Here are the commands I use:

ian@ubuntu:~/Desktop/NASM/Program4$ nasm -f elf -o asm.o SystemCalls.asm ian@ubuntu:~/Desktop/NASM/Program4$ gcc -o program asm.o ian@ubuntu:~/Desktop/NASM/Program4$ ./program

I don't get any errors, however nothing is printed to the terminal. I used the following link to ensure the registers contained the correct values: http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html

4

3 回答 3

1

您必须将整数值转换为字符串才能使用sys_write(系统调用 4)打印它。转换可以这样完成(未经测试):

; Converts the integer value in EAX to a string in
; decimal representation.
; Returns a pointer to the resulting string in EAX.
int_to_string:
  mov byte [buffer+9],0  ; add a string terminator at the end of the buffer
  lea esi,[buffer+9]
  mov ebx,10             ; divisor      
int_to_string_loop:
  xor edx,edx            ; clear edx prior to dividing edx:eax by ebx
  div ebx                ; EAX /= 10
  add dl,'0'             ; take the remainder of the division and convert it from 0..9 -> '0'..'9'
  dec esi                ; store it in the buffer
  mov [esi],dl
  test eax,eax
  jnz int_to_string_loop    ; repeat until EAX==0
  mov eax,esi
  ret

buffer: resb 10
于 2013-09-16T18:45:08.157 回答
0

汇编编程需要了解 ASCII 代码和一些基本的转换例程。例如:十六进制到十进制,十进制到十六进制是保存在某个存储空间的好例程。没有寄存器可以按原样打印,您必须转换(很多)。更有帮助:ASCII 0 什么都不打印,但一些文本编辑器(kde linux 中的 kate)会在屏幕上显示一些东西(正方形或...)。在 C 和 C++ 等高级语言中,它用于指示 NULL 指针和字符串的结尾。也可用于计算字符串长度。10 是行尾。取决于 Linux 或 Windows,是否也会有回车(Linux)(Windows/Dos)。13 是回车 1B 是 ESC 键(Linux 用户现在会更多地了解这个) 255 是硬回车,我不知道为什么它有好处,但它必须有它的目的。查看http://www.asciitable.com/获取整个列表。

于 2015-02-12T11:14:55.340 回答
-1

将整数值转换为字符串。在这里,我使用宏打包和解包将整数转换为字符串,并使用宏解包反之亦然

%macro write 2 
    mov   eax, 4
    mov   ebx, 1
    mov   ecx, %1
    mov   edx, %2
    int   80h
%endmacro

%macro read 2 
    mov eax,3
    mov ebx,0
    mov ecx,%1
    mov edx,%2
    int 80h
%endmacro


%macro pack 3   ;    1-> string ,2->length ,3->variable
    mov esi, %1
    mov ebx,0
    %%l1:
    cmp byte [esi], 10
    je %%exit

    imul ebx,10
    movzx edx,byte [esi]
    sub edx,'0'
    add ebx,edx
    inc esi
    jmp %%l1
    %%exit:
    mov  [%3],ebx
%endmacro

%macro unpack 3    ; 1-> string ,2->length ,3->variable
    mov esi, %1
    mov ebx,0
    movzx eax, byte[%3]
    mov byte[%2],0

    cmp eax, 0
    jne %%l1
    mov byte[%2],1
    push eax
    jmp %%exit2

    %%l1:
    mov ecx,10
    mov edx,0
    div ecx
    add edx,'0'
    push edx
    inc byte[%2]
    cmp eax, 0
    je %%exit2

    jmp %%l1 

    %%exit2:
    movzx ecx,byte[%2]
    %%l2:
    pop edx
    mov [esi],dl
    inc esi
    loop %%l2
%endmacro

section .data       ; data section
msg1:   db "First number : "    ; 
len1:   equ $-msg1      ; 
msg2:   db "Second number : "   ; 
len2:   equ $-msg2      ;                   
msg3:   db "Sum : " ; 
len3:   equ $-msg3      ;   
ln: db 10
lnl: equ $-ln
var1: resb 10
var2: resb 10
str1: resb 10
str2: resb 10
ans: resb 10
ansvar: resb 10
ansl: db ''
l1: db ''
l2: db ''

section.text        ;code
global _start
_start:
    write msg1,len1
    read str1,10      
    pack str1,l1,var1

    write msg2,len2
    read str2,10      
    pack str2,l2,var2    

    mov al,[var1]
    add al,[var2]

    mov [ansvar],al
    unpack ans,ansl,ansvar
    write msg3,len3
    write ans,10
    write ln,lnl

    mov ebx,0       ; exit code, 0=normal
    mov eax,1       ; exit command to kernel
    int 0x80        ; interrupt 80 hex, call kernel

要汇编程序,链接并运行:

nasm -f elf add.asm 
ld -s -o add add.o
./add 
于 2016-04-27T15:41:53.597 回答