Home > Back-end >  Assembly language: AL=max(AL,BL,CL)
Assembly language: AL=max(AL,BL,CL)

Time:11-08

I have to calculate the max value from 3 values using registers, and store the max value in AL. It is required to use jumps and compares.
For example I wrote how to determine the max value from 2 numbers, but I can't do it using 3.

mov bl,1
mov cl,2
cmp bl,cl
jg label1   ;jump if greater
mov al,bl   ;al keeps the maximum
jmp stop

  label1: mov al,bl
  stop:nop;

I should do the AL=max(AL,BL,CL) after this model.

CodePudding user response:

Currently your code will always report BL for greatest! The line mov al,bl ;al keeps the maximum should read mov al, cl.

For AL=max(AL,BL,CL), first find the max between AL and BL and keep it in AL that is to be modified anyway as it will be the result. Then find the max between the possibly new AL and CL yielding the final AL:

  cmp al, bl
  jg  OK1
  mov al, bl
OK1:
  cmp al, cl
  jg  OK2
  mov al, cl
OK2:
  ; AL is max(AL,BL,CL)

This way registers BL and CL are not modified, if that's important.

  • Related