nasm汇编:通过输入当前和出生年份来计算年龄

时间:2015-08-22 05:38:28

标签: assembly nasm digits subtraction

我需要一些帮助,我们被要求制定一个计划,通过输入出生年份和当前年份来计算我们的年龄。例如:

  

---预期产出:

     

输入当前年份:2015

     

输入出生年份:1996

     

输出:19

     

---我的电流输出

     

(1)输入当前年份:2015

     

输入出生年份:1996

     

输出:1

     

(2)输入当前年份:2015

     

输入出生年份:2000

     

输出:0

     

(3)输入当前年份:2015

     

输入出生年份:0013

     

输出:2

这里是代码,我是一个完整的初学者,所以任何帮助都会很棒

section .data
   presentYear db 'Enter Current Year: ', 10
   presentYearLen equ $-presentYear

   birthYear db 'Enter Birth year: ', 10
   birthYearLen equ $-birthYear

   newLine db 10
   newLineLen equ $-newLine

section .bss
   num1 resw 1
   num2 resw 1
   result resw 1


section .text
   global _start
_start:
   ;prints message to get current year
   mov eax, 4
   mov ebx, 1
   mov ecx, presentYear
   mov edx, presentYearLen
   int 80h

   ;gets input
   mov eax, 3
   mov ebx, 0
   mov ecx, num1
   mov edx, 5
   int 80h

   ;prints message to enter birth year
   mov eax, 4
   mov ebx, 1
   mov ecx, birthYear
   mov edx, birthYearLen
   int 80h

   ;gets input
   mov eax, 3
   mov ebx, 0
   mov ecx, num2
   mov edx, 5
   int 80h

   ;convert inputs to decimal for arithmetic
   sub word [num1], 30h
   sub word [num2], 30h

   ;puts inputs in registers
   mov ax, [num1]
   mov bx, [num2]
   ; get complement of 2nd number for subtraction
   neg bx
   ; subtract
   add ax, bx

   ;put the result to result variable
   mov [result], ax 

   ;convert back to ascii code
   add word [result], 30h

   ;print difference
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 2
    int 80h


    ;terminate
    mov eax, 1
    mov ebx, 0
    int 80h

0 个答案:

没有答案