首页 > 解决方案 > 添加 2 个数字并让程序显示 2 位数字时遇到问题

问题描述

我需要修改这个程序才能打印出一个两位数。输入数字在 1-9 之间,输出是两个个位数数字的和,所以输出应该在 0 到 18 之间。老师的提示是输入应该是 00-18 的格式。我尽我所能翻译了它。

cr equ 13 ; Vognretur
lf equ 10 ; Linjeskift
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
STDERR equ 2

; Datasegment
section .bss
siffer resb 4

; Datasegment
section .data
   meld db "Skriv to ensifrede tall skilt med mellomrom.",cr,lf
      db "Summen av tallene maa vaere mindre enn 10.",cr,lf
meldlen equ $ - meld
feilmeld db cr,lf, "Skriv kun sifre!",cr,lf
feillen equ $ - feilmeld
crlf db cr,lf
crlflen equ $ - crlf

; Codesegment with program
section .text

global _start
_start:
mov edx,meldlen
mov ecx,meld
mov ebx,STDOUT
mov eax,SYS_WRITE
int 80h

; Read number, read number returns in ecx
; Succesfull return if edx=0
call lessiffer
cmp edx,0 ; Test
jne Slutt 
mov eax,ecx ; First digit saved in reg eax

call lessiffer
; Read second digit
; success: edx=0, number in ecx
cmp edx,0 ;Test 
jne Slutt
mov ebx,ecx ; second digit saved in reg ebx

call nylinje
add eax,ebx
mov ecx,eax
call skrivsiffer ; Print out value in ecx as a single digit number

Slutt:
mov eax,SYS_EXIT
mov ebx,0
int 80h

; ---------------------------------------------------------
skrivsiffer:
; Prints digit saved in ecx. No value check
push eax
push ebx
push ecx
push edx
add ecx,'0' ; converter tall til ascii.
mov [siffer],ecx
mov ecx,siffer
mov edx,1
mov ebx,STDOUT
mov eax,SYS_WRITE
int 80h
pop edx
pop ecx
pop ebx
pop eax
ret

; ---------------------------------------------------------
lessiffer:
; searches past blank spaces to next non-blank
; Next non-blank is returned ti ecx
  push eax
  push ebx

Lokke:
  ; Reads sign from keyboard
  mov eax,SYS_READ
  mov ebx,STDIN
  mov ecx,siffer
  mov edx,1
  int 80h
  mov ecx,[siffer]
  cmp ecx,' '
  je Lokke
  cmp ecx,'0' ; checks if digit is between 0-9
  jb Feil
  cmp ecx,'9'
  ja Feil
  sub ecx,'0' ; converts ascii to number.
  mov edx,0 ; signals successfull input
  pop ebx
  pop eax
  ret ; Vellykket retur

Feil:
  mov edx,feillen
  mov ecx,feilmeld
  mov ebx,STDERR
  mov eax,SYS_WRITE
  int 80h
  mov edx,1 ; singaled failed input
  pop ebx
  pop eax
  ret ; failed return

; ---------------------------------------------------------
; Move cursor to the left on next line
nylinje:
  push eax
  push ebx
  push ecx
  push edx
  mov edx,crlflen
  mov ecx,crlf
  mov ebx,STDOUT
  mov eax,SYS_WRITE
  int 80h
  pop edx
  pop ecx
  pop ebx
  pop eax
  ret

; End _start

有很多代码,我知道,但是因为我是汇编新手,我不太确定在哪里修改。我猜想修改将在 skrivsiffer 函数中。

标签: assemblyx86

解决方案


推荐阅读