Implement C=|A-B| with inc,dec,jnz (A,B are non-negative)

前端 未结 3 1105
时光说笑
时光说笑 2021-01-29 15:09

This is a question I saw in an interview :

A,B are non-negative numbers and you need to return C=|A-B| where you have only the following instructions:

3条回答
  •  梦毁少年i
    2021-01-29 15:20

    Just decrement both in a loop until one of them gets to be zero. The other is obviously the result.

        inc a
        inc b      ; make sure input is not zero
    loop:
        dec a
        jnz skip
        dec b
        mov eax, b ; return b as answer
        ret
    skip:
        dec b
        jnz loop
        mov eax, a ; return a as answer
        ret
    

提交回复
热议问题