1

We have the following task: write a program that sets the default values of x and y (in the program these are 3 and 5 respectively), then it compares these numbers and replaces the lesser of them with the half-sum of x and y, and replaces the greater of them with twice the multiplication of x and y.

section .data
x dw 3
y dw 5

section .text
global _start

_start:
  ; Сравним значения x и y
  mov ax, [x]
  cmp ax, [y]
  
  ; Если x <= y, переходим к метке less_than
  jle less_than
  
  ; Если x > y, переходим к метке greater_than
  jmp greater_than

less_than:
  ; Найдем полусумму x и y и заменим меньшее значение на нее
  mov bx, [x]
  add bx, [y]
  shr bx, 1
  mov [x], bx
  
  ; Умножим большее значение на 2
  mov bx, [y]
  shl bx, 1
  mov [y], bx
  
  ; Выведем значения x и y
  mov ah, 02h ; функция вывода символа на экран
  mov dl, byte [x] ; выводим x
  add dl, '0' ; преобразуем в символ
  int 21h ; вызов BIOS
  
  mov ah, 02h ; функция вывода символа на экран
  mov dl, byte [y] ; выводим y
  add dl, '0' ; преобразуем в символ
  int 21h ; вызов BIOS
  
  ; Завершаем программу
  mov ah, 4Ch
  xor al, al
  int 21h
  
greater_than:
  ; Найдем полусумму x и y и заменим меньшее значение на нее
  mov bx, [x]
  add bx, [y]
  shr bx, 1
  mov [y], bx
  
  ; Умножим большее значение на 2
  mov bx, [x]
  shl bx, 1
  mov [x], bx
  
  ; Выведем значения x и y
  mov ah, 02h ; функция вывода символа на экран
  mov dl, byte [x] ; выводим x
  add dl, '0' ; преобразуем в символ
  int 21h ; вызов BIOS
  
  mov ah, 02h ; функция вывода символа на экран
  mov dl, byte [y] ; выводим y
  add dl, '0' ; преобразуем в символ
  int 21h ; вызов BIOS
  
  ; Завершаем программу
  mov ah, 4Ch
  xor al, al
  int 21h

When I compile my program, I get just "Pp" when I should get 4 and 30 respectively. screenshot of just the Pp

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Mizless
  • 11
  • 2
  • 3
    You seem to use SMALL memory model but you didn't initialize `DS` to section `.data`. *multiplication of x and y* is missing. Your simplified method of conversion binary to decimal digit works only for numbers below 10. – vitsoft Mar 12 '23 at 12:16

1 Answers1

1

One of your results (4 and 30) has more than 1 digit. You need a better way of outputting the multi-digit numbers. See Displaying numbers with DOS.


then it compares these numbers and replaces the lesser of them with the half-sum of x and y, and replaces the greater of them with twice the multiplication of x and y

  • It would be best to not start by comparing the numbers and then have to duplicate a lot of code. First calculate the replacement expressions and keep the results in registers. (eg. mov [x], bx would destroy x before you could use it next)

      mov bx, [x]
      add bx, [y]
      shr bx, 1      ; -> BX is "half-sum of x and y"
    
      mov ax, [x]
      mul word [y]
      shl ax, 1      ; -> AX is "twice the multiplication of x and y"
    
  • Then compare the numbers and load x and y accordingly:

        mov  cx, [x]
        cmp  cx, [y]
        jl   less_than
        xchg ax, bx    ; Just swapping the new values
      less_than:
        mov  [x], bx
        mov  [y], ax
    

replaces the lesser of them

The jle less_than instruction is branching on LessOrEqual. Following the task description that really should become jl less_than.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76