F
F
fuse_1502022-03-02 20:40:49
assembler
fuse_150, 2022-03-02 20:40:49

Why does division of div fail in assembler in C++?

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

int main()
{
  setlocale(LC_ALL, "rus");
  char b1 = 3, b2, b3 = 1 , res2; //1 байт el, bl, ah    ]]  cbw -> short
  short w1= 1000, w2 = 500, w3 = 2; //2 байт ax, bx, cx, dx,   cwde -> long
  long d1 = 5000, res; //4 байт eax, ebx, ecx   cbq -> дальше 
  
  printf("%d %d %d %d\n", w1, w2, w3, d1);
  _asm{
    //    1     2    3    8    7      4     6     5 
    //(w1 - w2) * w3 / b1 + b2 * ((d1 - w2) - (w3 + b3));
    // 1 - (w1 - w2) 
    mov ax, w1
    sub ax, w2
    // 2 - (w1 - w2) * w3
    imul ax, w3
    // 3 - (w1 - w2) * w3 / b1
    mov bl, b1 
    div bl 
    mov res2, al

    

    
     
    

    
    
  
  }
  
  printf("%d", res2);
  
  
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2022-03-02
@fuse_150

Why does division of div fail in assembler in C++?
char b1 = 3, b2, b3 = 1 , res2; //1 байт el, bl, ah    ]]  cbw -> short
  short w1= 1000, w2 = 500, w3 = 2;
//(w1 - w2) * w3 / b1

div bl

Because (1000 - 500) * 2 / 3 = 333, and this does not fit into one byte of the result, which means you get an exception. See _

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question